We were working on a requirement last week to pull a report of all vRealize Automation Managed Machines using PowervRA Module version 3.7.0 which supports vRealize Automation 7.6. While generating the report using a PowerShell script we noticed that the output was only showing the name of the Machine owner instead of the ID.
Then we started digging into the function Get-vRAResource of PowervRA Module and we noticed that the method which is used by function Get-vRAResource in PowervRA Module is /catalog-service/api/consumer/resourceViews

After checking the vRealize Automation Catalog Service API 7.6 documentation we realized that this method was deprecated in vRealize Automation 7.5 version and this method is similar to /catalog-service/api/consumer/resources method.

We used our Good Old API building Platform Postman to do a side by side comparison of vRA API methods /catalog-service/api/consumer/resourceViews and /catalog-service/api/consumer/resources. If you’ll look at the output of both the methods closely, you’ll notice that the resourceViews methods returns only the Name of the Owner where as the resources methods returns an Array which includes the values of tenantName, ref, type & value out of which ref is the Machine Owner’s ID.

Armed with all that information we prepared our script to pull resource Information of all vRA Managed Machines using PowervRA function Get-vRAResource and then we used the vRealize Automation API method /catalog-service/api/consumer/resources/{ResourceID} to get the Machine Owner’s ID of each Resource to generate our vRA Managed VMs report with Owner’s ID.
Script:
Set-ExecutionPolicy RemoteSigned
$cred = Get-Credential
$vRAFQDN = "vRA_FQDN"
$vRATenant = "TenantName"
#Connecting to vRA Server using PowervRA 3.7.0 Module
Connect-vRAServer -Server $vRAFQDN -Tenant $vRATenant -Username $cred.UserName -Password $cred.Password -IgnoreCertRequirements
#Fetching List of vRealize Automation Managed Virtual Machines
$filter = Get-vRAResource | where {$_.ResourceType -eq "Infrastructure.Virtual"}
$date = Get-Date -Format "yyyy-MM-dd-hh-mm-tt"
$print = $filter | Select Data,Owners,ResourceId
$output = $print | Select @{Name="VMName";Expression={$_.Data.MachineName}}, @{Name="BusinessGroup";Expression={$_.Data.MachineGroupName}},`
@{Name="Owner";Expression={$_.Owners}}, @{Name="OwnersID";Expression={""}}, @{Name="ResourceID";Expression={$_.ResourceId}}, @{Name="ReservationName";Expression={$_.Data.MachineReservationName}},`
@{Name="vCPUs";Expression={$_.Data.MachineCPU}}, @{Name="Memory (MB)";Expression={$_.Data.MachineMemory}}, @{Name="Storage (GB)";Expression={$_.Data.MachineStorage}}
$output = $output | where {$_.VMName -ne $null}
$output | ft -AutoSize -Wrap
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')
$Body = @{
username = $cred.UserName
password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($cred.Password))
tenant = $vRATenant
} | ConvertTo-Json
#Generating API Bearer Token for vRA Login
$url = "https://" + $vRAFQDN
$tokenurl = $url + "/identity/api/tokens"
$token = Invoke-RestMethod -Method Post -Uri $tokenurl -Body $Body -Headers $headers -Verbose
$token = $token.id
$headers.Add('Authorization',"Bearer $token")
$printvalue = @{}
$i=0;
#Get Machine Owner's ID for each Managed Machine
foreach ($item in $output)
{
$owneridurl = $url + "/catalog-service/api/consumer/resources/" + $item.ResourceId
$ownersId = Invoke-RestMethod -Method Get -Headers $headers -uri $owneridurl -Verbose
$item.OwnersID = $ownersId.owners.ref
$printvalue[$i] = $item
$i = $i + 1
}
$printvalue.Values | FT -AutoSize -Wrap
$printvalue.Values | Export-Csv C:\Users\Administrator\Desktop\vRAManagedMachineReport-$date.csv -NoTypeInformation
Output:

Just replace the vRA_FQDN and TenantName with the details of your vRA environment and supply the output file path to generate the report.
Happy Scripting!!
