Last active
August 12, 2021 06:49
-
-
Save brianbunke/72604b5a1bbdd14b19ba5feeed381be2 to your computer and use it in GitHub Desktop.
Learning the vSphere 6.5 REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vSphere 6.5 VCSA - REST API testing | |
$vCenterName = 'YOURNAMEHERE' | |
$BaseURI = "https://$vCenterName/rest" | |
# At first, I could only make this work with vSphere SSO credentials | |
# Try that if AD auth is throwing 401 Unauthorized errors at you | |
# (Discovered later that it didn't like the colon in my AD user's password) | |
# Can also try "https://YOURVCENTER/apiexplorer" in your browser to test login creds | |
$c = Get-Credential -UserName 'administrator@vsphere.local' -Message 'Enter password' | |
# Construct Base64 header for Basic Authentication | |
$utf8 = [System.Text.Encoding]::UTF8.GetBytes($c.UserName+':'+$c.GetNetworkCredential().Password) | |
$auth = [System.Convert]::ToBase64String($utf8) | |
$head = @{'Authorization' = "Basic $auth"} | |
# Receive and store session ID token for further calls | |
$r = Invoke-WebRequest -Uri "$BaseURI/com/vmware/cis/session" -Method Post -Headers $head | |
$token = (ConvertFrom-Json $r.Content).value | |
$session = @{'vmware-api-session-id' = $token} | |
# The fun begins: (looks like you always need to extract the value property from responses?) | |
# Get compute clusters | |
(Invoke-RestMethod -Uri "$BaseURI/vcenter/cluster" -Method Get -Headers $session).value | |
# Get hosts | |
(Invoke-RestMethod -Uri "$BaseURI/vcenter/host" -Method Get -Headers $session).value | |
# "https://YOURVCENTER/apiexplorer" in a browser for all available endpoints | |
# An example to view all available GET methods in the CLI | |
# Credit to LucD at http://techgenix.com/vmware-rest-api/ | |
# Note that "/com/vmware" is required here but not for the vcenter REST calls | |
# Would love to know more about this area of querying available endpoints from the command line | |
(Invoke-RestMethod -Uri "$BaseURI/com/vmware/vapi/metadata/cli/command" -Method Get -Headers $session).value | | |
Where-Object Name -eq get | Sort-Object path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment