Automate NSX Logical Switch Creation Using PowerNSX

Automate NSX Logical Switch Creation Using PowerNSX

This is a post about how to mass create logical switches for VMware NSX. First we will show how to create NSX Logical Switches using the GUI. While easy enough, when it comes to very large deployments requiring hundreds or even thousands of logical switches for VXLAN/Layer 2 broadcast domain segmentation, a faster method would be idea.

First, this tutorial will require PowerCLI and PowerNSX to be installed on your system. If you’ve never installed PowerNSX, use the following link from the PowerNSX GitHub website for the Windows Installer. PowerNSX Windows Installation Link

Create NSX Logical Switch in vSphere Web Client

1. Go to “Networking and Security” and get to the NSX Home Page. Click the “Logical Switches” Tab.

2. Click the “+” icon. Give your logical switch a Name, Description. Select the Desired Transport Zone. Choose the desired replication mode, and desired settings for IP Discovery and MAC Learning. Click “OK” when done.

And that’s it. Your new logical switch has been created.

Pretty easy right. But again the theme of this blog is automation and scale. What happens when we need to create 50, 100, 500, 2000 logical switches? Clicking through those menus will likely get old pretty quickly. Now lets look at mass creating Logical Switches using a custom script for Power NSX.

Create Multiple NSX Logical Switches using PowerNSX. 

1. Download the script and csv file from the zip file below. The script is also shown below for easy copy and paste if desired.

PowerNSX_LogicalSwitch

## Automate NSX Logical Switch Creation
## Author: Terrance Levon Johnson
## Substitute Variable information in lines 6 through 11 with information that matches your environment.
## Point Variable in line 13 to the file location of the Logical_Switch_Creation csv file.   

$NSXManagerIP = "192.168.0.176"
$VCenterIP = "192.168.0.78"
$NSXManagerUser = "admin"
$NSXManagerPassword = "Password"
$VCenterUser = "[email protected]"
$VCenterPassword = "VCenterPassword"

$NSXLOGICALSWITCHFILE = import-csv C:\work\NSX_PowerNSX\Logical_Switch_Creation.csv

Connect-NsxServer -Server $NSXManagerIP -Username $NSXManagerUser -Password $NSXManagerPassword -ViUserName $VCenterUser -ViPassword $VCenterPassword

$LS = 0

foreach ($LogicalSwitch in $NSXLOGICALSWITCHFILE)
{
## Creates Logical Switches for NSX. Possible Control Plane Modes are UNICAST_MODE,HYBRID_MODE,MULTICAST_MODE
Get-NsxTransportZone | New-NsxLogicalSwitch -Name $LogicalSwitch.LogicalSwitchName -ControlPlaneMode UNICAST_MODE

$LS++
Write-Progress -Activity "Creating Logical Switches" -status "Created: $LS of $($NSXLOGICALSWITCHFILE.Count)" -PercentComplete (($LS / $NSXLOGICALSWITCHFILE.Count) * 100)

}

Write-Host "!!!All Logical Switches Created Sucsessfully" -ForegroundColor Green

2. Unzip the file and open the Logical_Switch_Creation csv file. Enter the names of the desired logical switches in the csv file. In this example we will create 54 logical switches representing their respective VXLAN. Save the .csv file and exit.

3. Now edit the “NSX_Create_Logical_Switch.ps1” file. You want to open the file in the Windows PowerShell ISE windows. 

4. Edit the variables in lines 6 through 11 to match your environment. Edit line 13 to point the location of the logical_switch_creation csv file on your machine. Also Note lines 21 and 22 if you want to change the logical switch replication mode from Unicast, Multicast or Hybrid. In this example, we will leave the replication mode as Unicast.  Save and close when finished.

5. Launch PowerCLI and browse to the location of the script. My script and csv file is located in the c:\work\nsx_powernsx directory.

6. Execute the NSX_Create_Logical_Switch.ps1 file by typing “./NSX_Create_logical_Switch.ps1”. ( Make sure the PowerShell ExecutionPolicy is set to Unrestricted before running the file if it’s not already. “Set-ExecutionPolicy Unrestricted” ) This will proceed to create all of the desired logical switches.

Now if we check the VMware Web Console, we can see all of our newly created NSX logical switches.

And that’s it. We have just create 54 logical switches for NSX. Using this method, we could plan for a massive deployment by getting logical switch/VXLAN names pre-deployment. At deployment we can run this script and get the logical switching portion of the NSX environment setup quickly and efficiently. Thanks for reading.

Author: johnter23