Using the REST API with PowerShell
PowerShell has a built-in Invoke-WebRequest
cmdlet that can be used to send HTTP and HTTPS requests to a web service. In this recipe, we will demonstrate how to leverage the NSX REST API through PowerShell using the Invoke-WebRequest
cmdlet.
Getting ready
You should have a workstation with Windows PowerShell or PowerShell Core installed.
How to do it...
This recipe is divided into two parts of REST API requests using PowerShell: retrieving a configuration or information using HTTP GET
, and creating a logical switch using HTTP POST
.
Requesting the HTTP GET REST API via PowerShell
In this example, we will perform an HTTP GET REST API request via PowerShell:
- Open a text editor, input the following code, and save it into a
.ps1
extension, for exampleNSX-PowerShellGET.ps1
:
# NSX Variables $NSXUsername = "admin" $NSXPassword = "VMware1!" $NSXManager = "https://nsxmgr-01a.corp.local" $NSXURI = "/api/2.0/services/usermgmt/user/admin" # NSX Authorization Header...