To use this function add it to the class library file named vRealizeAutomation.ts
GitHub Repository: https://github.com/SimonSparksUK/Orchestrator
Filename: vRealizeAutomation.ts
Function addCustomPropertiesToVM
Description: The following code add Custom Properties to a VCACEntity Virtual Machine.
publicaddCustomPropertiesToVM (objVCACEntityVirtualMachine:VCACEntity,objProperties:Properties):void
{
letarrVCACEntityVirtualMachineProperty:VCACEntity[]=objVCACEntityVirtualMachine.getLink(this.objVCACHost,"VirtualMachineProperties");
letarrKey:string[]=objProperties.keys;
letobjPropertiesLinks:Properties=newProperties();
objPropertiesLinks.put("VirtualMachine",objVCACEntityVirtualMachine);
letobjPropertiesToAdd:Properties=newProperties();
objPropertiesToAdd.put("IsHidden",false);
objPropertiesToAdd.put("IsRuntime",false);
objPropertiesToAdd.put("IsEncrypted",false);
letstrVirtualMachineName:string=objVCACEntityVirtualMachine.getProperty("VirtualMachineName");
arrKey.forEach((strKey:string):void=>
{
letstrValue:string=objProperties.get(strKey);
objPropertiesToAdd.put("PropertyName",strKey);
objPropertiesToAdd.put("PropertyValue",strValue);
System.log("'"+strVirtualMachineName+"' - Creating Custom Property '"+strKey+"' to '"+strValue+"'.");
try
{
letobjVCACEntityVirtualMachineProperty:VCACEntity=arrVCACEntityVirtualMachineProperty.find((objVCACEntityVirtualMachineProperty:VCACEntity):VCACEntity=>
{
if (objVCACEntityVirtualMachineProperty.getProperty("PropertyName")==strKey)
{
System.log("'"+strVirtualMachineName+"' - Found Existing Custom Property '"+strKey+"'.");
returnobjVCACEntityVirtualMachineProperty;
}
});
letobjPropertiesHeaders:Properties=newProperties();
if (objVCACEntityVirtualMachineProperty)
{
letobjEntityKey:Properties=objVCACEntityVirtualMachineProperty.entityKey;
letstrEntityKeyId:string=objEntityKey.get("Id");
System.log("'"+strVirtualMachineName+"' - Updating Custom Property '"+strKey+"' with value '"+strValue+"'.");
vCACEntityManager.updateModelEntityBySerializedKey(this.objVCACHost.id,"ManagementModelEntities.svc","VirtualMachineProperties",strEntityKeyId,objPropertiesToAdd,objPropertiesLinks,objPropertiesHeaders);
}
else
{
System.log("'"+strVirtualMachineName+"' - Creating Custom Property '"+strKey+"' with value '"+strValue+"'.");
vCACEntityManager.createModelEntity(this.objVCACHost.id,"ManagementModelEntities.svc","VirtualMachineProperties",objPropertiesToAdd,objPropertiesLinks,objPropertiesHeaders);
}
}
catch (objException)
{
System.warn("'"+strVirtualMachineName+"' - Failed to Create Custom Property '"+strKey+"' to '"+strValue+"'.");
System.error(objException);
}
System.log("'"+strVirtualMachineName+"' - Successfully Created Custom Property '"+strKey+"' to '"+strValue+"'.");
});
}Discover more from Cloud Build Tools
Subscribe to get the latest posts sent to your email.
