logo

How to use PowerShell to send REST-API calls to vRA

Doing REST API calls via Powershell is straight forward using the Invoke-RestMethod cmdlet. Using the examples from inkysea’s blogpost, I’ll be showing how to do this calls to vRA using Powershell.

To start off, we need to define the header

$header = @{ "Accept" = "application/json"; "Content-Type" = "application/json"  }

and the body payload

$body = @{
username = "account";
password= "password";
tenant = "vsphere.local"
}

 

After which, we define the URI/ vRA URL to pass the REST payload

$URI = "https://<VRA URL>/identity/api/tokens"

We then invoke the Invoke-RestMethod cmdlet

$token = Invoke-RestMethod -Method Post -Uri $uri -Headers $header -Body (ConvertTo-Json $body)
  • ConvertTo-Json was done in $body variable because the REST call expects a JSON payload
  • We save the response to $token variable as we need the ID/ authentication

 

Once completed, you will receive the TOKEN ID that we will be using to issue additional commands via REST.

NOTE: this ID expires after 24 hours. If you want to change the duration, this can be controlled via the property  identity.basic.token.lifetime.hours=N  of the /etc/vcac/security.properties file.

token

 

 

Once we have the token, we re-add the TOKEN ID in the header payload

$header.Add("Authorization", "Bearer " + $token.id)

Store the URI for the catalog service

$catalog = "https://<VRA URL>/catalog-service/api/consumer/entitledCatalogItems"

Invoke RestMethod cmdlet to get the Entitled Catalog item for the user

$response = Invoke-RestMethod -Method Get -Uri $catalog -Headers $header -UseDefaultCredentials:$false
  • -UseDefaultCredential is needed as i keep getting authorization error (for some weird reason) so I had to hardcode this part
  • $reponse would contain all entitled resource for the particular user

 

that’s it!

Enjoy!

Leave a Reply

*

captcha *