The following blog post originated on the personal blog of VMware Sr. Systems Engineer, Vinith Menon. With his permission, I am reposting this content here on the official VMware Cloud Management blog. In this post, Vinith provides a way to access the vRealize Operations internal APIs via Powershell. The public APIs can be accessed through the PowerCLI module for vRealize Operations, but there are some useful APIs to explore in the internal API set. Be aware that internal APIs are subject to change or removal with any new release, so use at your own risk.
I hope you enjoy this repost of Vinith’s article on using vR Ops internal REST APIs with Powershell.
Access Internal REST APIs in vROPS with Powershell
Recently we had a challenge with one of our customer where we had about 2000 custom groups which had to be created in vR Ops, and it had to be done fast. So there was no time for manual efforts. The REST API exposed for this in an internal API for vR Ops and the sad part was that PowerCLI, the tool that i love and can solve almost anything, does not have these exposed at this time.
Hence, I resorted to the Powershell way to invoke REST APIs
The Powershell Invoke-RestMethod
If you explore the vROPS internal API reference guide, you can find details on the XML body which needs to be passed for this invocation.
Once you get this request format, you need to build an XML body to be passed to create this custom group. But the tough part is how you get the values for what you need to pass. For this I basically created a custom group manually and then I sent a GET request to the internal API to identify the contents of the XML and voila, I got the details of the XML node data which needs to be passed along with the POST request.
First, identify the API call from the API guide.
Next, I created the REST call using Invoke-RestMethod.
#Create URL string for Invoke-RestMethod $urlsend = 'https://' + 'vrops.vmwareebc.in' + '/suite-api/internal/resources/groups' #Credential $cred = Get-Credential ## Debug echo $urlsend #Send Attribute data to vRops. $ContentType = "application/xml;charset=utf-8" $header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $header.Add("Accept", 'application/xml') $header.Add("X-vRealizeOps-API-use-unsupported", 'true') try { $result = Invoke-RestMethod -Method GET -uri $urlsend -Credential $cred -ContentType $ContentType -Headers $header } catch { $result = $_.Exception.Response.GetResponseStream() $reader = New-Object System.IO.StreamReader($result) $reader.BaseStream.Position = 0 $reader.DiscardBufferedData() $responseBody = $reader.ReadToEnd(); } Write-Output $result.InnerXml
The results for this would be saved in $result.InnerXML.
Next I figured out which data I required.
</ops:group><ops:group identifier="f3316192-42ed-40e5-9f44-068cdc9af241"><ops:resourceKey><ops:name>BIS Branch - 07380</ops:name><ops:adapterKindKey>Container</ops:adapterKindKey><ops:resourceKindKey>BIS - Branch Codes</ops:resourceKindKey><ops:resourceIdentifiers /></ops:resourceKey><ops:membershipDefinition><ops:includedResource s></ops:includedResources><ops:excludedResources></ops:excludedResources><ops:rule-group><ops:resourceKindKey><ops:reso urceKind>AppSession</ops:resourceKind><ops:adapterKind>V4V</ops:adapterKind></ops:resourceKindKey><ops:attributeRules xsi:type="ops:metric-key-condition" key="client|machine_name"><ops:stringValue>07380</ops:stringValue><ops:compareOpera tor>CONTAINS</ops:compareOperator></ops:attributeRules></ops:rule-group></ops:membershipDefinition></ops:group><ops:gro up identifier="d5b0741e-1478-4f2f-b2e8-c315990a1cf6"><ops:resourceKey><ops:name>Unlicensed Group</ops:name><ops:adapter KindKey>Container</ops:adapterKindKey><ops:resourceKindKey>Licensing</ops:resourceKindKey><ops:resourceIdentifiers /></ ops:resourceKey><ops:membershipDefinition><ops:includedResources></ops:includedResources><ops:excludedResources></ops:e xcludedResources></ops:membershipDefinition></ops:group>
And once I got the required values for the nodes from the above content, I created the POST request to invoke this.
#Create XML Structure and populate variables from the dump $XMLFile = @('<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ops:group xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/">> <ops:resourceKey> <ops:name>BIS Branch -11041</ops:name> <ops:adapterKindKey>Container</ops:adapterKindKey> <ops:resourceKindKey>BIS - Branch Codes</ops:resourceKindKey> </ops:resourceKey> <ops:membershipDefinition> <ops:rule-group> <ops:resourceKindKey> <ops:resourceKind>VirtualMachine</ops:resourceKind> <ops:adapterKind>VMWARE</ops:adapterKind> </ops:resourceKindKey> <ops:attributeRules xsi:type="ops:metric-key-condition" key="cpu|usage_average"> <ops:compareOperator>CONTAINS</ops:compareOperator> <ops:stringValue>11041</ops:stringValue> </ops:attributeRules> </ops:rule-group> </ops:membershipDefinition> </ops:group>' ) [xml]$xmlSend = $XMLFile #Create URL string for Invoke-RestMethod $urlsend = 'https://' + 'vrops.vmwareebc.in' + '/suite-api/internal/resources/groups' ## Debug echo $urlsend #Send Attribute data to vRops. $ContentType = "application/xml;charset=utf-8" $header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $header.Add("Accept", 'application/xml') $header.Add("X-vRealizeOps-API-use-unsupported", 'true') try { $result = Invoke-RestMethod -Method POST -uri $urlsend -Body $xmlSend -Credential $cred -ContentType $ContentType -Headers $header } catch { $result = $_.Exception.Response.GetResponseStream() $reader = New-Object System.IO.StreamReader($result) $reader.BaseStream.Position = 0 $reader.DiscardBufferedData() $responseBody = $reader.ReadToEnd(); }
Now if you check $result, you should see the response for the POST method.
Now if you check your vR Ops dashboard you can see the custom group created successfully! Now with the magic of for-loop I can iterate it across 2000 objects and get totally automated!
I hope you enjoyed this blog post and found this useful for accessing Internal REST APIs in vR Ops with Powershell.
Vinith Menon has been in the IT industry since 2007, and has been working with Windows PowerShell since 2009. He currently works at VMware as a Senior Cloud Automation Engineer on Virtualization, Automation and Microsoft Applications. He has been awarded the Microsoft MVP for 2014,2015 and 2016, VMware vExpert 2016 and Sapien MVP from Sapien. He blogs at Virtualize & Automate and can be contacted on Twitter @vinithmenon28.