Bulk creation of conditional custom groups using vROPs API in under 5 Minutes

Hello,

Recently had a task of creating a dashboard for a customer where we had to create over 110 custom groups based on different Project and Sub-project mapping.
The next task was to give permissions to correct AD users to these groups. Definitely knowing how Lazy I am, I was not going to do it manually as there were over 8000 VMS.
Lucky for me insomnia kicked in and I happen to stumble upon the REST API for creating custom groups.

Note: you can get the complete explanation and video on my blog and youtube channel. Following are the steps I followed to create custom groups to be used in dashboard.

STEP 1 : Tagging the VMs in vCenter –
This was a simple task probably a one liner using a CSV file.

New-TagAssignment -Entity $vm -Tag $Project
New-TagAssignment -Entity $vm -Tag $SubProject

There were total of 4 TAGs we created – Project Name, Sub Project Name, Environment and Owner of VM (Owner needed for permissions)

Step 2 : Script for creating custom group

Go to https://<vrops fqdn>/suite-api

you can find the entire list of supported REST API

api1

So now we know the API is a POST method and the URL is /api/resources/groups               Next is looking at the Input format XML, we can use the same XML given in the example

xml1

I used the same XML to send in the body, next step was to test if it works after modification You can do the same using our good friend Postman

postman

Once the test was successful it was just matter of putting it all together in a script and BAM!!!! I had all 110 custom groups ready even before I could get a coffee

$cmdbInput = import-csv -path “C:\Temp\test.csv” -header (“VMName”, “ProjectName”, “SubProject”, “Environment”, “Application”, “Owner”);

$XMLFile = @(‘

<ops:custom-group autoResolveMembership="false" xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/” xmlns:xs=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“>

_GroupName_

Container

Environment

VirtualMachine

VMWARE

_Project_

CONTAINS

_SubProject_

CONTAINS

‘)

 

 

 

foreach($CI in $cmdbInput)

{

$vm = $CI.VMName;

$Project = $CI.ProjectName;

$SubProj = $CI.SubProject;

$ENV = $CI.Environment;

$Owner = $CI.Owner;

$App = $CI.Application;

 

$groupName = $Project+”-“+$SubProj;

 

 

 

$XMLUnique = $XMLFile

$XMLUnique = $XMLUnique.Replace(“_GroupName_”, $groupName)

$XMLUnique = $XMLUnique.Replace(“_Project_”, $Project)

$XMLUnique = $XMLUnique.Replace(“_SubProject_”, $SubProj)

 

Write-host $XMLUnique

 

[xml]$xmlSend = $XMLUnique

 

#Create URL string for Invoke-RestMethod

$urlsend = ‘https://https:///suite-api/internal/resources/groups&#8217;

 

## 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’)

Invoke-RestMethod -Method POST -uri $urlsend -Body $xmlSend -Credential $cred -ContentType $ContentType -Headers $header

 

#CleanUp Variables to make sure we dont update the next object with the same data as the previous one.

Remove-Variable urlsend -ErrorAction SilentlyContinue

Remove-Variable xmlSend -ErrorAction SilentlyContinue

Remove-Variable XMLFile -ErrorAction SilentlyContinue

 

}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s