« Back to Microsoft Discussions

UCS Mass Vlan Provisioning Powershell Script

Combination View Flat View Tree View
Threads [ Previous | Next ]
Hello everyone,
 
I wrote a script that does mass vlan provisoning using the UCS Powershell module.  I noticed there isn't much script sharing around here so I thought I'd contribute.  I've attached the script along with an example .csv to this post.  I hope someone is able to find a use for it.  Feel free to modify it to suite your needs and let me know if you have any comments or questions about it.
 
# This script requires the UCS PowerShell Module Found at: http://developer.cisco.com/web/unifiedcomputing/pshell-download
# Purpose: Auto-provisions VLANs for the UCS Environment
# Note: The Vlans are Added Globally and to all vNics
# Usage: ./Provision-Vlans.ps1 -UCSM (IP OR FQDN) -CSV (.csv)
# Written By: Philip Sautter
PARAM(
    [Parameter(Mandatory=$true,HelpMessage="Please enter the IP address or the fully qualified domain
    name of the UCSM you would like to connect to.")]
    [ValidateNotNullOrEmpty()]
    [System.String]$UCSM
    ,[Parameter(Mandatory=$true,HelpMessage="Please enter the path to the .csv file containing the
    vlan list.  No path or ./ may be used if both files reside in the same directory.  The .csv should consist of two columns
    (name,id) separated by a single comma.  The vlan name should not include any white space or special characters.  The vlan
    ID should be a number between 1-3976 or 4049-4093")]
    [ValidateNotNullOrEmpty()]
    [System.String]$CSV
)
# Start Variable Declariation
$InvalidChars = '`','~','!','@','#','$','%','^','&','*','(',')','+','=','|','\',']','[','}','{',';','?','/','>','<',','
$InvalidPattern = ::join('|', ($invalidChars | % {::escape($_)}))
# End Variable Declaration

# Default Installation Path for the UCS PowerShell Module
Import-Module "C:\Program Files (x86)\Cisco\UCSMPowerToolkit\CiscoUCSPS.dll"

# Connect to the UCSM, Popup Window for Credentials
Connect-UCSM $UCSM

# Start Processing Vlan List
$VlanImport = Import-Csv $CSV | %{
    $VlanId = $_.Id
    # Validate .CSV Contains Entries for Both Vlan Name and ID
    IF (($_.Name -eq "") -or ($VlanId -eq "")) {
        Write-Warning "
        Either the Vlan Name or ID is null.  Please review the .csv for the following line: $_.
        This item is being skipped.  All other items will still be processed."
    }# End IF
    ELSE {
        # Validate the Vlan is in the Proper Range
        IF ((($VlanId -ge 1) -and ($VlanId -le 3976)) -or (($VlanId -ge 4049) -and ($VlanId -le 4093))) {
            # Remove all Unaccepted Characters from Vlan Name / Replace White Space with Underscore
            $VlanName = $_.Name -replace $InvalidPattern -replace ' ','_'
            # Add the Vlan to the Fabric Interconnects
            Add-Vlan -VLANName $VlanName -ID $VlanId -FI Dual
            # Start Processing the Service Profiles
            Get-ServiceProfile | %{
                $SPName = $_.Name
                # Start Processing vNics
                Get-Vnic -SPName $SPName | %{
                # Add Vlan to Each vNic Without Promting User
                Set-Vnic -VnicName $_.Name -SPName $SPName -VLANName $VlanName -Force
                }# End Processing vNics
            }# End Processing Service Profiles
        }# End IF
        ELSE {
            Write-Warning "
            The Vlan ID $_. is not valid.  Please ensure the value is between 1 and 3976 or 4049 and 4093.
            This item is being skipped.  All other items will still be processed."
        }# End ELSE
    }# End ELSE
}# End Processing Vlan List
# Disconnect from the UCSM
Disconnect-UCSM
Attachments: