VMware NSX has an open API and it’s pretty easy to consume. PowerShell is the same way; it’s easy to learn and easy to extend. This week, Anthony Burke and Nick Bradford released a PowerShell extension called PowerNSX. As the name suggests, it’s all about managing VMware NSX.

Nick and Anthony put PowerNSX on Bitbucket, which means there will be a continuos release cycle (whenever someone pushes something to the Git repository). You can download and install PowerNSX using Git or by downloading the branch as a zipfile.

Prerequisites

To run PowerNSX, you need a couple of things pre-installed on your machine. PowerShell v3+, .NET 4.5+, PowerCLI and the PowerNSX Module. The good news is that PowerNSX provides an installer script that you can use to use to install all of those at once. It’s as easy as executing the installer script, on which it will look for the prerequisites and only download and install the packages that are not installed yet. Here’s what it does:

powernsx-installation

Using PowerNSX

After installing the PowerNSX module, you can start using the NSX commandlets. Firstly include the PowerCLI Snap-in, then load the PowerNSX module and connect to your NSX Manager.

PS D:\Powershell\PowerNSX> Add-PSSnapIn vmware.vimautomation.core
PS D:\Powershell\PowerNSX> Import-Module -Name 'C:\Program Files\Common Files\Modules\PowerNSX\PowerNSX.psm1'
WARNING: The names of some imported commands from the module 'PowerNSX' include unapproved verbs that might make them
less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose
parameter. For a list of approved verbs, type Get-Verb.
PS D:\Powershell\PowerNSX> Connect-NsxServer -Server nsx-manager.lab.local -username admin -password secretpassword

PowerNSX requires a PowerCLI connection to the vCenter server NSX is registered against for proper operation.
Automatically create PowerCLI connection to vcenter.lab.local?
[Y] Yes [N] No [?] Help (default is "N"): Y

Enter credentials for vCenter vcenter.lab.local

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential

Version : 6.2.0
BuildNumber : 2986609
Credential : System.Management.Automation.PSCredential
Server : nsx-manager.lab.local
Port : 443
Protocol : https
ValidateCertificate : False
VIConnection : vcenter.lab.local

PS D:\Powershell\PowerNSX>

The vCenter configuration is automatically detected from the settings on the NSX Manager and a connection is made to make it easier for PowerNSX to work with vCenter objects such as virtual machines, clusters, dvs, etc.

Examples

When you’re connected to both the NSX Manager and vCenter, you can start doing all kinds of fun stuff. For instance, getting all the NSX Logical Switches in object form:

PS D:\Powershell\PowerNSX> Get-NsxTransportZone | Get-NsxLogicalSwitch
objectId : virtualwire-1
objectTypeName : VirtualWire
vsmUuid : 423015A2-0DA3-F354-866F-5FF34B0D43BD
nodeId : 423015A2-0DA3-F354-866F-5FF34B0D43BD
revision : 3
type : type
name : Edge-to-LDR
description :
clientHandle :
extendedAttributes :
isUniversal : false
universalRevision : 0
tenantId : virtual wire tenant
vdnScopeId : vdnscope-1
vdsContextWithBacking : vdsContextWithBacking
vdnId : 5000
multicastAddr : 239.0.0.0
guestVlanAllowed : false
controlPlaneMode : HYBRID_MODE
ctrlLsUuid : 9878701a-18de-4b1a-827b-db333277c777
macLearningEnabled : false

Or creating and removing Logical Switches:

PS D:\Powershell\PowerNSX> Get-NsxTransportZone -Name TZ_Testlab | New-NsxLogicalSwitch -Name New_Logical_Switch
objectId : virtualwire-31
objectTypeName : VirtualWire
vsmUuid : 423015A2-0DA3-F354-866F-5FF34B0D43BD
nodeId : 423015A2-0DA3-F354-866F-5FF34B0D43BD
revision : 2
type : type
name : New_Logical_Switch
description :
clientHandle :
extendedAttributes :
isUniversal : false
universalRevision : 0
tenantId :
vdnScopeId : vdnscope-1
vdsContextWithBacking : vdsContextWithBacking
vdnId : 5013
multicastAddr : 239.0.0.13
guestVlanAllowed : false
controlPlaneMode : HYBRID_MODE
ctrlLsUuid : 50ccca8b-0318-4005-8d0d-a7cd5710311f
macLearningEnabled : false

PS D:\Powershell\PowerNSX> Get-NsxTransportZone -Name TZ_Testlab | Get-NsxLogicalSwitch New_Logical_Switch | Remove-NsxLogicalSwitch

Logical Switch removal is permanent.
Proceed with removal of Logical Switch New_Logical_Switch?
[Y] Yes [N] No [?] Help (default is "N"): Y
PS D:\Powershell\PowerNSX>

Conclusion

If you are working with VMware NSX, then PowerNSX should definitely be in your toolbox. It’ll make your life a lot easier and make configuring NSX faster. Go get it!



Share the wealth!

Leave a Reply

Your email address will not be published. Required fields are marked *