Again helping the community
I had some spare time and it’s been a while since I did anything with the vROps API. Until I checked theĀ VMware community
This sounds like something fun to do. So here is the outcome!
<#
Author: Kabir Ali - info@kablog.nl
Scriptname: VF (vCenterFinder)
Version: 1.1 (Tested)
Date: Dec 18 2020
Why: bridrod asked for it @ https://communities.vmware.com/t5/vRealize-Operations-Manager/Finding-a-VM-in-vROPS-and-vCenter-VM-belongs-to-using-Powershell/m-p/2874272/emcs_t/S2h8ZW1haWx8dG9waWNfc3Vic2NyaXB0aW9ufEtWNlRCQ0tMSlg2V08yfDI4NzQyNzJ8U1VCU0NSSVBUSU9OU3xoSw#M18803
Remark: Test setup
vROps Version 8.4.0 (17863947)
Update: 1.1 - filter on only virtualMachines - line 68
#>
<#
Example:
.\VF.ps1 -vROpsServer "vROps.local.domain" -vROpsUser "admin" -vROpsPass "VMware1!" -VMName "Ubuntu"
#>
Param (
[Parameter(Mandatory = $true)][string]$vROpsServer,
[Parameter(Mandatory = $true)][string]$vROpsUser,
[Parameter(Mandatory = $true)][string]$vROpsPass,
[Parameter(Mandatory = $true)][string]$VMName
)
# Adding certificate exception to prevent API errors
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
# Building vROps API string & invoking REST API
$BaseURL = "https://" + $vROpsServer + "/suite-api/api/"
$BaseAuthURL = "https://" + $vROpsServer + "/suite-api/api/auth/token/acquire"
$Type = "application/json"
# Creating JSON for Auth Body
$AuthJSON =
"{
""username"": ""$vROpsUser"",
""password"": ""$vROpsPass""
}"
# Authenticating with API
Try {
$vROpsSessionResponse = Invoke-RestMethod -Method POST -Uri $BaseAuthURL -Body $AuthJSON -ContentType $Type
}
Catch {
$_.Exception.ToString()
$error[0] | Format-List -Force
}
# Extracting the session ID from the response
$vROpsSessionHeader = @{"Authorization"="vRealizeOpsToken "+$vROpsSessionResponse.'auth-token'.token
"Accept"="application/json"}
# Get VM's unique identifier
Try {
$VMNameURI = $BaseURL+"resources?resourceKind=virtualmachine&name=" +$VMName
$VMNameID = (Invoke-RestMethod -Method GET -Uri $VMNameURI -Headers $vROpsSessionHeader -ContentType $Type).resourceList.identifier
}
Catch {
$_.Exception.ToString()
$error[0] | Format-List -Force
}
# Get VM's vCenter
Try {
$VMvCenterURI = $BaseURL+"resources/properties?resourceId="+$VMNameID
$VMvCenter = ((Invoke-RestMethod -Method GET -Uri $VMvCenterURI -ContentType $Type -Headers $vROpsSessionHeader).resourcePropertiesList.property | where {$_ -like "*summary|parentVcenter*"}).value
}
Catch {
$_.Exception.ToString()
$error[0] | Format-List -Force
}
# Output
Write-Host "Found VM:" $VMName "on vCenter:" $VMvCenter
# Close sessions and done
Invoke-RestMethod -Method POST -Uri $BaseURL"/auth/token/release" -Headers $vROpsSessionHeader -ContentType $Type

Good morning, I tried using your script this morning, but I get a lot of “(401) Unauthorized” errors. I noticed you only allow for a username and password, which I am guessing is only for local users. When I connect to vROps in the web interface, I have to specify the domain in an additional field. When using Connect-OMServer, I have to add the -AuthSource ‘DOMAIN’ switch. Any way to specify this in the API calls?
Hi Rob,
Sorry for the late reply, I somehow didn’t get a notification.
Yes I’m using a local account. If you want to use a 3rd party authsource then I need to change the code for that. Let me know if you still need help.
Kind regards,
Kabir