I have a Powershell script that calls the REST api to get a list of machines I have access to and their status. For some reason no matter what the machines actual status is, whether ON, OFF, Shutting Down, or what have you, the status I get back from the API always just says ACTIVE.
What am I missing?
My end goal is to be able to call the API and get a list of machine that are still running. I'm looking for a couple machines in particular for reasons I won't get into, but if those machines are still running, I will execute the next script to call the API to shut them down.
I have the shut down script working fine, but for now I cannot actually get the status using a script.
Below is my script to get the machine listing. I feel like I must be missing something really obvious.
function Get-VRACToken { param( [string] $username, [string] $pass, [string] $vracHost = "MyHost.mydomain.com" ) $uri = "https://$vracHost/identity/api/tokens" if(!$pass) { $cred = Get-Credential $username = "$($cred.GetNetworkCredential().Domain)\$($cred.GetNetworkCredential().UserName)" $pass = $cred.GetNetworkCredential().Password } $body =@{ username=$username password=$pass tenant="vsphere.local" } $body = $body | ConvertTo-Json $header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $header.Add("accept","application/json") $response = Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -Headers $header -Body $body Write-Output $response.id } function Get-VRACMachine { param( [string] $token, [string] $vracHost = "MyHost.mydomain.com", [validateSet("name","full","summary")] [string] $outPutType = "summary", [string] $machineName = "*" ) $header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $header.Add("Authorization","Bearer $token") $header.Add("accept","application/json") $uri = "https://$vracHost/catalog-service/api/consumer/resources/?page=1&limit=100" $response = Invoke-RestMethod -Uri $uri -Headers $header switch ($outPutType) { "name" {Write-Output $response.content | Where name -like $machineName | Select name} "full" {Write-Output $response.content | Where name -like $machineName} "summary" {Write-Output ($response.content | Where name -like $machineName | Select name,id,status,organization)} } } $token = Get-VRACToken Get-VRACMachine -token $token