Accessing NSX API from Powershell

While pounding my head over trying to figure out the proper authentication and header settings to configure when calling the NSX REST API from Powershell I came across a post on Chris Wahl’s blog that included a function that already had all that worked into it. It didn’t take much to re-purpose for my needs -a function that (when passed an API call) return the XML response as a Powershell object.

My version includes hard-coded admin credentials which is fine in my hermetically sealed environments but most admins might want to keep Chris’ passed-authentication method.

Retrieve the settings for a specific edge, by polling the list of all edges for a specific name$NS = NSXApi ("https://192.168.110.42/api/4.0/edges/" + ($edges.pagedEdgeList.edgePage.edgeSummary | where {$_.name -eq "North-South"}).objectid)

Note that the API guide doesn’t list all the possible calls you can make, for example how to retrieve the global accessgroup/security group configuration. However, you can piece things together from similar topics,

While the GET string to retrieve the list of all security groups is not listed, the string to get specific ones isGET https:///api/2.0/services/applicationgroup/<applicationgroup-id>

Terrific, but how do you get a list of all of them?

In several places “globalroot-0” is listed as a top-level designation, sticking that on the end like so:

Love your script. However, the function for the Interfaces doesn’t work due to the XML object model.

I had to change the ForEach loop: $rxml.edge.interfaces.interface

FYI: This is a result of the XML object tree inside the NSX environment not being “standard”. However, using Chrome’s RESTAPI and the manual commands to pull the config on the specific edge, I was able to easily figure out the tree structure and make changes.

This script is inspiring. I am looking for getting the xml content from NSX edge application and check if particular entry is available, update if not using powershell and push through REST call. Any help appreciated.