Creating NSX API Calls with PowerShell

I’ve been spending a fair amount of time working with Python for API calls to various things and wondered how easy it would be in PowerShell. Although I’m familiar with the [net.webRequest] method, it’s a bit clunky for PowerShell and is not a cmdlet.

Fortunately, PowerShell 3.0 and later ships with two similar yet different commands: Invoke-WebRequest and Invoke-RestMethod. From what I can tell, the main difference is that Invoke-RestMethod returns the content of the results while Invoke-WebRequest returns all sorts of information (status, headers, etc.).

Armed with Invoke-WebRequest, I decided to start with a simple call to NSX Manager to retrieve some handy data from my controllers. Although I wrote a fair bit about NSX APIs in my Working with NSX blog series, this is my first PowerShell cmdlet that calls the NSX API, so it’s a bit of a “first run” for me. I’m hoping that it will be an example to build upon for your scripts and mine. You can view the code and my comments below, or download it from my Scripts repo on GitHub.


  • Lines 29-41: Useful if you’re using PowerShell version 4 and have do not trust the certificate issued by the server. Feel free to comment out for real world scenarios where you do trust it.
  • Line 44: Basic auth requires a Base64 string of username:password.
  • Line 45: This creates the key:value pair (System.Collections.Hashtable) for the header request. This simple GET request just needs an Authorization key.
  • Line 48: The specific URI of what I’m trying to gather; in this case, it’s controller data.
  • Line 51: I define $rxml as an xml class to make parsing the content easier later.
  • Lines 54-65: A simple splat to pull in some interesting data. There’s mountains of values you can pull in.
  • Line 67: The results are pumped into the console window with a format table (ft). Nothing fancy; you can make it do more interesting things if you want. :)