Continuing on from my previous post about vRops addProperties, I will now give you an example of how to create Custom Group‘s and populate them automatically with objects…
Like the previous script’s I have provided, the one below is intended to help not give you a full solution… the originals I have created contain logic specific to my needs. (lookup existing custom groups, etc…)
So to start off in the vRops GUI, I create a new group type called Criticality, the purpose of which will be used to structure and contain the custom groups which the script below will generate from unique values in the csv file.
Metadata.csv sample
NAME,FUNCTION,COSTCODE,CRITICALITY LINSRV1,WEB,12,LOW LINSRV2,RANDOM,18,LOW LINSRV3,APP,18,LOW NSXMGR,NSX Manager,55,MISSION CRITICAL pup,PUPPET,123,LOW vRA,vCAC-vRA,88,LOW vrep,vRealize Replication,67,LOW vsdp,vRealize Data Protection,67,LOW vVNX,EMC-VNX-LAB,67,N/A WEBSRV2,XAMPP-DR,99,NORMAL WEBSRV3,XAMPP-PROD,99,CRITICAL WINSRV2,"VC,UPDATEMGR",99,MISSION CRITICAL WINSRV3,"AD,DNS,DHCP,GW",99,MISSION CRITICAL WINSRV4,"WSUS,DNS,AD",99,LOW WINSRV5,WDS,99,LOW WINSRV6,SCCM,99,LOW WINSRV7,Minecraft-SRV,99,LOW WINSRV8,vRops-6.3,99,CRITICAL WINWS1,WIN10,5,LOW
So… to the script!
Update the script with your own vRops server IP or FQDN.
$vRopsAddress = 'vrops.vMan.ch'
Like the previous scripts… so you don’t keep being prompted for credentials, save the service account user and password with the powershell Get-Credential command.
$cred = Get-Credential $cred | Export-Clixml -Path "d:\vRops\home.xml"
update the XML to reflect the file generated above…
$cred = Import-Clixml -Path "$ScriptPath\Home.xml"
Update to point at the metadata.csv and update the XML structure to match what you need.
$AttributeImport = Import-csv "$ScriptPath\metadata.csv" | select CRITICALITY -Unique
update the XML for your needs…
ForEach($Group in $AttributeImport){ #Create XML Structure and populate variables from the Agility 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>{0}</ops:name> <ops:adapterKindKey>Container</ops:adapterKindKey> <ops:resourceKindKey>CRITICALITY</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:property-condition" key="VMAN|CRITICALITY"> <ops:compareOperator>EQ</ops:compareOperator> <ops:stringValue>{0}</ops:stringValue> </ops:attributeRules> </ops:rule-group> </ops:membershipDefinition> </ops:group>' -f $Group.CRITICALITY )
After the script finishes running you should now see the custom group’s which were generated.
The group(s) automatically update(s) the members from the properties of the objects, in this case VM’s. So as more VM’s are created and assigned with the property Criticality the group members are updated.
good luck! if you found the post helpful… please consider supporting my hosting by checking out one of the sponsored links.
Here is the full script,
#Vars $vRopsAddress = 'vrops.vMan.ch' $ScriptPath = (Get-Item -Path ".\" -Verbose).FullName [DateTime]$NowDate = (Get-date) [int64]$NowDateEpoc = Get-Date -Date $NowDate.ToUniversalTime() -UFormat %s $NowDateEpoc = $NowDateEpoc*1000 #Credentials Stuff $cred = Import-Clixml -Path "$ScriptPath\Home.xml" $vRopsUser = $cred.GetNetworkCredential().Username $vRopsPassword = $cred.GetNetworkCredential().Password #Take all certs. 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 #Import Metadata into a PSObject / table $AttributeImport = @() $AttributeImport = Import-csv "$ScriptPath\metadata.csv" | select CRITICALITY -Unique #Create XML, generate group ForEach($Group in $AttributeImport){ #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>{0}</ops:name> <ops:adapterKindKey>Container</ops:adapterKindKey> <ops:resourceKindKey>CRITICALITY</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:property-condition" key="VMAN|CRITICALITY"> <ops:compareOperator>EQ</ops:compareOperator> <ops:stringValue>{0}</ops:stringValue> </ops:attributeRules> </ops:rule-group> </ops:membershipDefinition> </ops:group>' -f $Group.CRITICALITY ) [xml]$xmlSend = $XMLFile #Create URL string for Invoke-RestMethod $urlsend = 'https://' + $vRopsAddress + '/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') 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 }
Great post. This helped me with the X-vRealizeOps-API-use-unsupported
requirement.
Have you tried creating a dynamic custom group using a rule like this?
Property – summary|vSphere tag – contains []
I am having some issues figuring out the correct json syntax to create this through the API.
I think it should be something like the example below…
I have been unsuccessful figuring it out. 🙁
I tried creating one manually and retrieving that as an example but having some issue there too and although I can successfully create a group via the API, it will not let me update it via the Web GUI to add the rule manually (internal server error). The group does work though.
@”
{
“resourceKey” : {
“name” : “AppIDGrouptest”,
“adapterKindKey” : “Container”,
“resourceKindKey” : “AppIDtype”,
“others” : [ ],
“otherAttributes” : {
}
},
“membershipDefinition” : {
“includedResources” : [ ],
“excludedResources” : [ ],
“rules” : [ {
“resourceKindKey” : {
“resourceKind” : “VirtualMachine”,
“adapterKind” : “VMWARE”
},
“attributeRules” : [ {
“Property-Condition” : {
“Key” : “Summary|vSphere Tag”,
“compareOperator” : “CONTAINS”,
“StringValue” : “[]”
}
}
],
“resourceNameRules” : [ ],
“relationshipRules” : [ ],
“others” : [ ],
“otherAttributes” : {
}
} ],
“others” : [ ],
“otherAttributes” : {
}
},
“others” : [ ],
“otherAttributes” : {
},
“identifier” : null
}
“@
Thanks, glad it was helpful. This group should be dynamic, it should add new members as they get the tags.. sorry I dont use JSON so i am not familiar with the structure..