VMware NSX PowerShell Extension Open Sourced
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:
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!
Related Posts
8 Comments
Leave a Reply Cancel reply
You must be logged in to post a comment.
Nice stuff!
Because we like to live dangerously use the -confirm:$false and avoid the prompts! Enjoy.
Oh and Nick deservers all the credit – he is the author! I am just the QA monkey.
Honestly, Anthony is a very overqualified Monkey, but thanks for the plug!
Can this be used with universal objects? maybe I don’t know which command use.
Thanks
Nelson
Hi Nelson,
This can indeed be used with universal objects, for instance the New-NsxTransportZone cmdlet has a -Universal switch and Get-NsxLogicalSwitch has a ‘isUniversal’ variable. Use Get-Help on the cmdlets you’d like to see universal support with and see if it’s in there.
Odd. I was able to create a new logical switch. When I ran the command to delete the switch everything pointed to the fact of a successful deletion – except it didn’t get deleted. It did state ‘Delete Distributed Port Group’ in vCenter tasks, but the logical switch remained. Even after refresh and log out/log in. I had to resort to manually deleting the switch.
That’s odd indeed. Did you try multiple executions? And did multiple tasks spawn?
If vCenter started a task to delete the LS, it sounds more like a vCenter/NSX issue then a PowerNSX issue as the API call was sent.
I can’t recall now if I tried multiple executions. It’s odd that I could create the switch via api call, but couldn’t delete it via another api call. If it was a vCenter/NSX issue I’d suspect the create api call would have failed.
Creating is something completely different then removing, so don’t consider them linked. Still, interesting observation, paging @pandom:disqus & @disqus_C5U0UNODhw:disqus to see if they’ve seen this before.