cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
215
Views
0
Helpful
1
Replies

PowerShell- Add a UCS vLAN Group to an existing vNIC Template

JDMils
Level 1
Level 1

I need to be able to copy the vLAN Groups from one vNIC Template to another using Powershell.

I have the following:

 

$UCSDomain = connect-ucs MyUCSDomain
$cloud = Get-UcsLanCloud
$oSourceTMP =  (Get-UcsVnicTemplate -Name "SHD-ESXi-A")[0]
$oTargetTMP =  Get-UcsVnicTemplate -Name "SHD-ESXi-A2"
$vlanGroups = (Get-UcsManagedObject -Dn $oSourceTMP.Dn -Hierarchy) | where-object {$_.rn -like 'net-group-ref*'}
$vLANGrp1 = $vlangroups[0]
# Try #1:
$oTargetTMP | Add-UCSFabricNetGroup $vlangrp1
# Try #2:
$oTargetTMP | Add-UCSFabricNetGroup -Ucs $UCSDomain -Name 'vGrp_XYZ'


 

My code keeps erroring with:

 

Add-UcsFabricNetGroup : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:15
+ ... TargetTMP | Add-UCSFabricNetGroup -Ucs $UCSDomain -ModifyPresent -Nam ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (Cisco.Ucsm.VnicLanConnTempl:PSObject) [Add-UcsFabricNetGroup], ParameterBind

 

Am I doing this correctly?

1 Reply 1

JDMils
Level 1
Level 1

Looks like this topic is VERY hard to fix, however I figured it out. The following code is an excerpt from my main working & tested code:

 

#
# Get source & target vNIC template names:
Param (
        [Parameter(Mandatory=$True)]
        [String]$SourcevNICTemplateName = "SHD-ESXi-A",
 
        [Parameter(Mandatory=$True)]
        [String]$TargetvNICTemplateName = "SHD-ESXi-A2",

        [Parameter(Mandatory=$False)]
        [Switch]$DebugCode,

        [Parameter(Mandatory=$False)]
        [String]$SourceTemplateDescription = ""    
    )

# Get the Source & Target vNIC Templates.
# If no template name is specified in the parameter $vNICTemplateName, then all templates are returned [$vNICTemplateName = '*'].
$oSourcevnicTemplate = Get-UcsVnicTemplate -Name $SourcevNICTemplateName -ErrorAction Stop
$oTargetvnicTemplate = Get-UcsVnicTemplate -Name $TargetvNICTemplateName -ErrorAction Stop

ForEach($vnicTemplate in $oSourcevnicTemplate)
    {
    # In the case that there exists more then one vNIC template with the same name, check which one the user wants to use
    # as the source vNIC template by the template description provided by the user at the start.
    # If the user did not provide a description then proceed anyway.
    If (($vnicTemplate.Descr -eq $SourceTemplateDescription) -or ([String]::IsNullOrEmpty($SourceTemplateDescription)))
        {
        # Get all VLAN Groups which have the same DN as the vNIC template.
        $vlanGroups = (Get-UcsManagedObject -Dn $vnicTemplate.Dn -Hierarchy) | where-object {$_.rn -like 'net-group-ref*'}
        # Get all vLANs which have the same DN as the vNIC template.
        $vLANsDirect = (Get-UcsManagedObject -Dn $vnicTemplate.Dn -Hierarchy) | where-object {$_.rn -like 'if-*'}
        
        # Adds vLANs from the source vNIC template to the target vNIC template.
        ForEach($vLANDirect in $vLANsDirect)
            {
            $vLANDirectName = $vLANDirect.Name
            # ****************************************************************************************************
            # Add the vLan to the new vNIC Template.
            # ****************************************************************************************************
            $Response = $oTargetvnicTemplate | Add-UcsVnicInterface -Name $vLANDirectName -ErrorAction Stop
            }
        # Cycle through all VLAN Groups.
        ForEach($vlanGroup in $vlanGroups)
            {
            $vLAN_Group_Name = $vlanGroup.Name
            # ****************************************************************************************************
            # Add vLAN Group into new vNIC template
            # ****************************************************************************************************
            $Response = $oTargetvnicTemplate | Add-UcsManagedObject -ClassId FabricNetGroupRef -PropertyMap @{Name=$vLAN_Group_Name;} -ErrorAction Stop
        } # If ($vnicTemplate.Descr -eq $SourceTemplateDescription)
        Else
            {
            If ($vNICTemplateCount -eq 0)
                {
                Write-Host "Source vNIC template with the stated description was not found." -ForegroundColor Red
                }
            Else
                {
                Write-Host "No further matching vNIC templates found."
                }
            Write-Host "Exiting." -ForegroundColor Red
            }
        }
    }

 

My issue is that previous UCSM admins created a bunch of vNIC templates with the same names throughout the org, and when I created a new SPT for M7 servers, and wanted to use a specific vNIC template, I could not as UCSM will only allow you to pick the last vNIC template in the org tree closest to your SPT. For example, if you have 3 vNIC templates in org above your SPT and one in the same org as your SPT, all with the same name, you can ONLY use the one in the same org as your SPT. This was confirmed by TAC as being "by design".

My script will allow you to pick one of the vNIC templates, any one, based on the name & description on the vNIC template, and copy the contents to a new vNIC template. You must create the new/target vNIC template first in UCSM in the org you want it to reside in.

Review Cisco Networking for a $25 gift card

Review Cisco Networking for a $25 gift card