Automate ‘Enable MAC Learning’ on Logical Switch with PowerNSX

I was working on a project at a customer where we needed to create Logical Switches, DFW rules and sections based upon all the current portgroups of all the environments. Because we are talking about +/- 400 logical switches which need to be created, this is not a job that you want to do via the vSphere Web Client. Therefore I created a script which exported all the current portgoup names and vlan configurations to CSV file. This CSV file is used to import all the stuff.

In our case we needed to adjust the naming convention, that’s the reason I first exported it to an external file which I was able to modify before importing it again. I will share this script later on via this blog. One requirement on the (Universal) Logical Switches was that MAC Learning was enabled on all of the Logical Switches in the environment. And at that moment I ran into a problem, since it is currently not (yet) possible to create a Logical Switch in PowerNSX with the -macLearningConfig feature enabled.

So, this is wat I did. First, as I mentioned before I did create all the logical switches, DFW sections. All the LS where in place, but not with the MAC Learning feature enabled. This is why I wrote a Powershell/PowerNSX script with invoking the API of the NSX manager.
Now I was able to set the MAC learning option to true in the API config, and I succesfully did this for all the +/- 400 logical switches within 5 minutes or so.

This is the script I’ve wrote:

##################################################################################################################################################
#### 
#### PowerNSX Script to invoke the NSX Manager API and enable Mac Learning on all the Logical Switched provided by an CSV file ####
#### Version: v1.0 
#### Contact: wgeelhoed at itq.nl 
#### Company: ITQ 
#### 
##################################################################################################################################################
##Infra-Info##
 $vcenterserver = 'vcsa01.wesleygeelhoed.local'
 $vcenteradmin = 'administrator@vwees.local'
 $vcenterpw = ''
 $csvpath = 'C:\pathto\virtualwires.csv'
Get-Module -ListAvailable VMware* | Import-Module
 Import-Module -Name PowerNSX
##Connection and details##
 Connect-VIServer $vcenterserver -user $vcenteradmin -Password $vcenterpw
 Connect-NsxServer -vCenterServer $vcenterserver -user $vcenteradmin -Password $vcenterpw
##For Primary NSX Manager##
 Get-NsxLogicalSwitch| Export-Csv -Path $csvpath
##For Secondary NSX Manager##
 #Get-NsxLogicalSwitch | Where-Object -Property isUniversal -EQ 'false' | Export-Csv -Path $csvpath
$lsexport = import-csv -path $csvpath
foreach ($item in $lsexport)
 {
 $virtualwireid = $item.objectId
 $logicalswitchname = $item.name
 write-host Enabling MAC Learning on Logical Switch $logicalswitchname which has the following virtualwire ID: $virtualwireid -ForegroundColor Yellow
 $xml = Invoke-NsxRestMethod -URI "/api/2.0/xvs/networks/$virtualwireid/features" -method get
 $xml.networkFeatureConfig.macLearningConfig.enabled
 $xml = '<networkFeatureConfig><ipDiscoveryConfig><enabled>true</enabled></ipDiscoveryConfig><macLearningConfig><enabled>true</enabled></macLearningConfig></networkFeatureConfig>'
 Invoke-NsxRestMethod -method put -uri "/api/2.0/xvs/networks/$virtualwireid/features" -body $xml
 }
 Write-host MAC Learning is now enabled on all of the Logical Switches in your NSX Environment -ForegroundColor Green

As you can see in the script there is a adjustment you need to make when you execute this script in a Cross vCenter NSX environment. That is why I created two different Export-CSV commands. Just comment out on of the two and you are ready to roll!

*this script will enable IP Discovery on the Logical Switches as well

sources: vmware.com, microsoft.com

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s