Authenticate to NSX Manager via API
When working with the NSX API, the first step is always authentication. Load up your NSX Manager FQDN (or IP address), along with the NSX Manager username and password as variables in your code. The following code will take your variables and convert them to base64 for consumption and authenticate to NSX Manager via API:
$nsx = "nsxmgr01.lab.local" $nsxuser = "admin" $nsxpass = "secretpassword" $uri = "https://$nsx" $creds = $nsxuser+":"+$nsxpass $bytes = [System.Text.Encoding]::ASCII.GetBytes($creds) $base64 = [System.Convert]::ToBase64String($bytes) $basicauth = "Basic $base64" $head = @{"Authorization"="$basicauth"}
The $head variable will encapsulate the necessary key:value pairs to authenticate against the NSX Manager. The output will be something similar to:
Authorization Basic HWRl9bks46c3V6dW908kkdmVIQ==
This is your authentication token. You can use this together with Invoke-WebRequest to make API calls to NSX Manager from PowerShell.
Here’s a link to the NSX API Guide (current version 6.4). I find it to be very useful when I need to reference a call.