How to add VLANS to Service Profiles in UCS Manager via PowerShell

We have had some unique cases where we need to add VLAN’s to a service profile’s vNic’s in the new Cisco UCS Manager. With the new HTML 5 Manager 3.1(2b) I am unable to make this change to all vNic’s templates. I have it push the change to all service profiles.

The solution was to write quick script that sets some variables, and adds the vlans to each vNic (in our case 2) for each service profile. I googled but was unable to find a quick and easy script. I decided to write my own and share it with you.

The requirements for this script to run properly are Cisco PowerTools and ensure that PowerShell remote execution is enabled.

Enjoy!

################################
# Author: Jeff Patton #
# #
# Date: 3/17/2017 #
################################

#Variables to be set by you.
$ucsserver = "172.0.0.20"
$hostsp = Import-CSV C:\Scripts\hosts.csv
$vlantoadd = "New-VLAN"
$identpoolA = "Pool-A"
$identpoolB = "Pool-B"

#Import Modules
Import-Module Cisco.UCSManager

You can create the VLAN’s via Gui or by using this script as well. If you want to create them in this script, remove the commented out sections called “VLAN Creation Section 1 and 2” in this script.

#VLAN Creation Section 1 - Connect to UCS Server
#Connect-Ucs $ucsserver -Credential (Get-Credential)

#VLAN Creation Section 2 - Optional Script to Add VLAN via CLI to Global LANCloud
#Get-UCSLanCloud | Add-VLAN -Name "DeleteMe" -Id "999"
#$vlantoadd = "DeleteMe"

#Script to Run
foreach ($part in $hostsp)
{
$sp = Get-UcsServiceProfile -Name $part.Name
$ethA = $sp | Get-UcsVnic -Name eth0 -IdentPoolName $identpoolA
$ethB = $sp | Get-UcsVnic -Name eth1 -IdentPoolName $identpoolB
$ethA | Add-UcsVnicInterface -Name $vlantoadd -Verbose
$ethB | Add-UcsVnicInterface -Name $vlantoadd -Verbose
}