Resolve vRA/vCAC VM to vCenter VM

I wrote this post some time ago but I felt that it didn’t include some other use cases where you would want to resolve a vCenter virtual machine. I also previously used code that was provided by the vRO appliance that I wasn’t too keen on (logging was a bit light and there was code to search using the BIOS UUID, which we don’t care about). I have therefore updated this post to reflect the following use cases:

  • Find the vCenter VM by its instance Uuid when using the Event Broker;
  • Find the vCenter VM by its instance Uuid on a specific vCenter Server;
  • Find the vCenter VM by its name, on a specific vCenter Server and a specific folder;
  • Find the vCenter VM by its MoRef id on a specific vCenter Server;

Hopefully, there should be enough code provided here to help you achieve your goal no matter what your scenario. However, if you need something different, then feel free to contact me and I will try to help.

All code that I have provided or talked about in this post can be downloaded as a vRO package for your consumption here.

Find the vCenter VM by its instance Uuid when using the Event Broker

If you want to perform configuration tasks against a vCenter VM during the deployment process or even for Day 2 operations, then you will need to resolve the vRA managed VM to a vCenter VM (of type VC:VirtualMachine) in your vRO workflows. Assigning vSphere Tags or Custom Attributes is a good use case for this.

The vRA/vCAC entity in the database has a property (VMUniqueID), which matches the vCenter Virtual Machine’s instance Uuid. We can therefore use this value to locate the vCenter VM. But in order to get this property, we first need to resolve the vCAC Entity. We can do this using the machine id that is sent by the Event Broker.

If you are unsure how to use the Event Broker, then I recommend you read this blog post, which covers it in detail: https://theithollow.com/2016/02/08/vrealize-automation-7-subscription.

We should now have a workflow that is ready to be used by the Event Broker Subscription, with an input of type ‘Properties’ (The name of the variable doesn’t matter). Our first action on the Workflow will retrieve the vCAC Entity id. Don’t worry if you find any of this difficult to follow as I’ll provide the workflow solution as part of the package included in the link at the beginning of this post.

getVirtualMachineIdFromEbsPayload

/*global ebsPayload*/

/**
 * Retrieves the virtual machine id from the event broker subscription payload.
 * @author Gavin Stephens <gavin.stephens@simplygeek.co.uk>
 * @version 1.1.0
 * @function getVirtualMachineIdFromEbsPayload
 * @param {Properties} ebsPayload - The event broker payload.
 * @returns {string} The virtual machine id.
 */

function checkParams(ebsPayload) {
    var inputErrors = [];
    var errorMessage;
    if (!ebsPayload || !(ebsPayload instanceof Properties)) {
        inputErrors.push(" - ebsPayload missing or not of type 'Properties'");
    }
    if (inputErrors.length > 0) {
        errorMessage = "Mandatory parameters not satisfied:\n" + inputErrors.join("\n");
        log.error(errorMessage);
    }
}

var logType = "Action";
var logName = "getVirtualMachineIdFromEbsPayload"; // This must be set to the name of the action
var Logger = System.getModule("com.simplygeek.library.util").logger(logType, logName);
var log = new Logger(logType, logName);
var virtualMachineId;

try {
    checkParams(ebsPayload);
    log.log("Getting virtual machine id from EBS Payload.");
    virtualMachineId = ebsPayload.machine.get("id");
    if (!virtualMachineId) {
        log.error("The virtual machine id was not found.");
    }
    log.log("Found virtual machine id '" + virtualMachineId + "'");
} catch (e) {
    log.error("Action failed to get virtual machine id from EBS payload.",e);
}

return virtualMachineId;

Now that we have the virtual machine id from the event broker, we need to query for the vcac virtual machine entity (this is the vm object within the vRA IaaS database). The following Actions may require the vCAC Host to be provided as an input and also make use of additional helper actions. All of these will be provided in the package included in the link at the beginning of this post.

findVirtualMachineEntityByVirtualMachineId

/*global vcacHost virtualMachineId*/

/**
 * Searches for a vcac virtual machine entity by its entity id.
 * @author Gavin Stephens <gavin.stephens@simplygeek.co.uk>
 * @version 1.1.0
 * @function findVirtualMachineEntityByVirtualMachineId
 * @param {vCAC:VCACHost} vcacHost - The vCAC Host.
 * @param {string} virtualMachineId - The virtual machine entity id.
 * @returns {vCAC:Entity} Returns the virtual machine entity.
 */

function checkParams(vcacHost, virtualMachineId) {
    var inputErrors = [];
    var errorMessage;
    if (!vcacHost || System.getObjectType(vcacHost) !== "vCAC:VCACHost") {
        inputErrors.push(" - vcacHost missing or not of type 'vCAC:VCACHost'");
    }
    if (!virtualMachineId || typeof virtualMachineId !== "string") {
        inputErrors.push(" - virtualMachineId missing or not of type 'string'");
    }
    if (inputErrors.length > 0) {
        errorMessage = "Mandatory parameters not satisfied:\n" + inputErrors.join("\n");
        log.error(errorMessage);
    }
}

var logType = "Action";
var logName = "findVirtualMachineEntityByVirtualMachineId"; // This must be set to the name of the action
var Logger = System.getModule("com.simplygeek.library.util").logger(logType, logName);
var log = new Logger(logType, logName);
var properties = new Properties();
var vcacVmEntity;
var vcacEntityFieldName = "VirtualMachineID";
var entitySetName = "VirtualMachines";
var vcacVmName = "";

try {
    checkParams(vcacHost, virtualMachineId);
    log.log("finding virtual machine entity with entity id: " + virtualMachineId);
    properties.put(vcacEntityFieldName, virtualMachineId);
    vcacVmEntity = System.getModule("com.simplygeek.library.vcac.entities").getVcacEntitiyByUniqieId(vcacHost,
                                                                                                     entitySetName,
                                                                                                     properties);
    vcacVmName = vcacVmEntity.getProperty("VirtualMachineName");
    log.log("Found virtual machine entity: " + vcacVmName + " with entity id: " + virtualMachineId);
} catch (e) {
    log.error("Action failed to locate vCAC virtual machine entity.",e);
}

return vcacVmEntity;

Now that we have the vCAC virtual machine entity, we need to retrieve the value for the ‘VMUniqueID’ property, which is the vCenter VM instance uuid.

getVmUniqueIdForVirtualMachineEntity

/*global vcacVmEntity*/

/**
 * Retrieves the value of the 'VMUniqueID' property for the provided vCAC virtual machine entity.
 * @author Gavin Stephens <gavin.stephens@simplygeek.co.uk>
 * @version 1.4.0
 * @function getVmUniqueIdForVirtualMachineEntity
 * @param {vCAC:Entity} vcacVmEntity - The vCAC virtual machine entity.
 * @returns {string} Returns the value for the VMUniqueID property.
 */

function checkParams(vcacVmEntity) {
    var inputErrors = [];
    var errorMessage;
    if (!vcacVmEntity || System.getObjectType(vcacVmEntity) !== "vCAC:Entity") {
        inputErrors.push(" - vcacVmEntity missing or not of type 'vCAC:Entity'");
    }
    if (inputErrors.length > 0) {
        errorMessage = "Mandatory parameters not satisfied:\n" + inputErrors.join("\n");
        log.error(errorMessage);
    }
}
var logType = "Action";
var logName = "getVmUniqueIdForVirtualMachineEntity"; // This must be set to the name of the action
var Logger = System.getModule("com.simplygeek.library.util").logger(logType, logName);
var log = new Logger(logType, logName);
var propertyKey = "VMUniqueID";
var propertyValue;

try {
    checkParams(vcacVmEntity);
    log.log("Retrieving property '" + propertyKey + "' from vm entity.");
    propertyValue = System.getModule("com.simplygeek.library.vcac.entities").getPropertyValueFromVcacEntity(vcacVmEntity, propertyKey);
    log.log("Found " + propertyKey + ": " + propertyValue);
} catch (e) {
    log.error("Action failed to get property from vm entity.",e);
}

return propertyValue;

At this stage we should finally have the vCenter VM instance uuid. We now need to make use of a final action that will use the vCenter vRO plugin, to search all the vCenter Server endpoints, for the virtual machine.

findVcVmByInstanceUuid

/*global vcVmInstanceUuid*/

/**
 * Search for a virtual machine by its instance uuid on all available vCenter Server endpoints.
 * @author Gavin Stephens <gavin.stephens@simplygeek.co.uk>
 * @version 1.1.0
 * @function findVcVmByInstanceUuid
 * @param {string} vcVmInstanceUuid - The vCenter virtual machine instance uuid.
 * @returns {VC:VirtualMachine} returns the vCenter virtual machine object.
 */

function checkParams(vcVmInstanceUuid) {
    var inputErrors = [];
    var errorMessage;
    if (!vcVmInstanceUuid || typeof vcVmInstanceUuid !== "string") {
        inputErrors.push(" - vcVmInstanceUuid missing or not of type 'string'");
    }
    if (inputErrors.length > 0) {
        errorMessage = "Mandatory parameters not satisfied:\n" + inputErrors.join("\n");
        log.error(errorMessage);
    }
}

var logType = "Action";
var logName = "findVcVmByInstanceUuid"; // This must be set to the name of the action
var Logger = System.getModule("com.simplygeek.library.util").logger(logType, logName);
var log = new Logger(logType, logName);
var vCenterVm;
var vCenterVmName = "";
var sdkConnections = [];
var found = false;
var vCenterSdkName;
var searchInVcDataCenter = null; // Set to vCDatacenter object to limit search or Null to search entire inventory.
var searchForVms = true; // false will search for hosts.
var searchByInstanceUuid = true; // false will search by BIOS UUID.

try {
    checkParams(vcVmInstanceUuid);
    sdkConnections = VcPlugin.allSdkConnections;
    if (sdkConnections.length > 0) {
        for (var i = 0; i < sdkConnections.length; i++) {
            log.log("Attempting to locate vcenter virtual machine with instance uuid '" + vcVmInstanceUuid + "'");
            vCenterVm = sdkConnections[i].searchIndex.findByUuid(searchInVcDataCenter, vcVmInstanceUuid, searchForVms, searchByInstanceUuid);
            if (vCenterVm) {
                vCenterVmName = sdkConnections[i].name;
                vCenterSdkName = sdkConnections[i].name;
                log.log("Found vcenter virtual machine '" + vCenterVmName + "' on vCenter '" + vCenterSdkName + "'");
                found = true;
                break;
            }
        }
    } else {
        log.error("No vcenter server endpoints were found to perform the search.");
    }
    if (!found) {
        log.error("The vcenter virtual machine could not be found with instance uuid '" + vcVmInstanceUuid + "'");
    }
} catch (e) {
    log.error("Action failed to locate vCenter virtual machine.",e);
}

return vCenterVm;

Here is what the workflow would look like. Remember, this is a sub-workflow that you would add to your master event broker subscribed workflow.

Get VC Virtual Machine from vRA EBS

[2019-01-30 21:45:23.908] [I] [Action: getVcacHost] Locating vCAC IaaS Host...
[2019-01-30 21:45:23.919] [I] [Action: getVcacHost] Found vCAC IaaS host: IaaS host for Default [https://web.vra.sgroot.local/]
[2019-01-30 21:45:23.950] [I] [Action: getVirtualMachineIdFromEbsPayload] Getting virtual machine id from EBS Payload.
[2019-01-30 21:45:23.976] [I] [Action: getVirtualMachineIdFromEbsPayload] Found virtual machine id 'cd9993fa-02f0-4050-9d4e-cc0ab4a85fa6'"
[2019-01-30 21:45:23.982] [I] [Action: getVirtualMachineEntitybyVirtualMachineId] Retrieving vCAC virtual machine entity with id: cd9993fa-02f0-4050-9d4e-cc0ab4a85fa6
[2019-01-30 21:45:23.990] [I] [Action: getVcacEntitiyByUniqieId] Retrieving vCAC Entity for set: VirtualMachines
[2019-01-30 21:45:24.067] [I] [Action: getVcacEntitiyByUniqieId] Successfully retrieved vCAC entity
[2019-01-30 21:45:24.081] [I] [Action: getVirtualMachineEntitybyVirtualMachineId] Found vCAC virtual machine entity: SG-BG-Deliv0003 with id: cd9993fa-02f0-4050-9d4e-cc0ab4a85fa6
[2019-01-30 21:45:24.103] [I] [Action: getVmUniqueIdFromVirtualMachineEntity] Retrieving VMUniqueID from vm entity.
[2019-01-30 21:45:24.118] [I] [Action: getVmUniqueIdFromVirtualMachineEntity] Found VMUniqueID: 501a6c09-35bc-1cf4-01ea-a01de06f2fc0
[2019-01-30 21:45:24.156] [I] [Action: findVcVmByInstanceUuid] Attempting to locate vcenter virtual machine with instance uuid '501a6c09-35bc-1cf4-01ea-a01de06f2fc0'
[2019-01-30 21:45:24.171] [I] [Action: findVcVmByInstanceUuid] Found vcenter virtual machine 'SG-BG-Deliv0003' on vCenter 'https://sg1-vsa001.sgroot.local:443/sdk'

You can now perform any scripting that you want against this VM and the changes will be reflected on the VM in vCenter.

Find the vCenter VM by its instance Uuid on a specific vCenter Server

I use these actions quite regularly in standard (non-event broker) workflows. When I am working with backup, DR or other solutions that require the vCenter VM, I like to locate these on the target vCenter Server and not search through all the endpoints. The reason for this is that 1) it’s quicker (not really a good enough reason though) and 2) I may have VMs across multiple vCenters with the same instance uuid.

The first action I have will dynamically discover the vCenter endpoint based on a site identifier. I include a site identifier in the vCenter Server hostname, which I use for the lookup. You may want to modify this action so that it better suites your requirements for dynamic discovery.

getVcenterSdkBySiteId

/*global siteId*/

/**
 * Finds a vCenter Server endpoint for the specified site id.
 * @author Gavin Stephens <gavin.stephens@simplygeek.co.uk>
 * @version 1.1.0
 * @function getVcenterSdkBySiteId
 * @param {string} siteId - The site identifier.
 * @returns {data_type} Returns the vCenter Server SDK Connection object.
 */

function checkParams(siteId) {
    var inputErrors = [];
    var errorMessage;
    if (!siteId || typeof siteId !== "string") {
        inputErrors.push(" - siteId missing or not of type 'string'");
    }
    if (inputErrors.length > 0) {
        errorMessage = "Mandatory parameters not satisfied:\n" + inputErrors.join("\n");
        log.error(errorMessage);
    }
}

var logType = "Action";
var logName = "getVcenterSdkBySiteId"; // This must be set to the name of the action
var Logger = System.getModule("com.simplygeek.library.util").logger(logType, logName);
var log = new Logger(logType, logName);
var vCenterSdkConnection;
var allVcenterSdks = [];
var vCenterSdkName = "";
var vCenterSdksFiltered = [];

try {
    checkParams(siteId);
    log.log("Locating vCenter Server with site id: " + siteId);
    allVcenterSdks = VcPlugin.allSdkConnections;
    vCenterSdksFiltered = allVcenterSdks.filter(function(x){return x.name.indexOf(siteId.toLowerCase()) !== -1;});
    if (vCenterSdksFiltered.length > 0) {
        vCenterSdkConnection = vCenterSdksFiltered[0];
        vCenterSdkName = vCenterSdkConnection.name;
        log.log("Found vCenter Server '" + vCenterSdkName + "'");
    } else {
        log.error("No vCenter Server found for site '" + siteId + "'");
    }
} catch (e) {
    log.error("Action failed locate a vCenter Server for the specified site id.",e);
}

return vCenterSdkConnection;

findVcVmByInstanceUuidOnVcenterSdk

/*global vCenterSDK vcVmInstanceUuid*/

/**
 * Search for a virtual machine by its instance uuid on a specific vCenter Server.
 * @author Gavin Stephens <gavin.stephens@simplygeek.co.uk>
 * @version 1.1.0
 * @function findVcVmByInstanceUuidOnVcenterSdk
 * @param {VC:SdkConnection} vCenterSdk - The vCenter Server SDK Connection.
 * @param {string} vcVmInstanceUuid - The vCenter virtual machine instance uuid.
 * @returns {VC:VirtualMachine} returns the vCenter virtual machine object.
 */

function checkParams(vCenterSDK, vcVmInstanceUuid) {
    var inputErrors = [];
    var errorMessage;
    if (!vCenterSDK || System.getObjectType(vCenterSDK) !== "VC:SdkConnection") {
        inputErrors.push(" - vCenterSDK missing or not of type 'VC:SdkConnection'");
    }
    if (!vcVmInstanceUuid || typeof vcVmInstanceUuid !== "string") {
        inputErrors.push(" - vcVmInstanceUuid missing or not of type 'string'");
    }
    if (inputErrors.length > 0) {
        errorMessage = "Mandatory parameters not satisfied:\n" + inputErrors.join("\n");
        log.error(errorMessage);
    }
}

var logType = "Action";
var logName = "findVcVmByInstanceUuidOnVcenterSdk"; // This must be set to the name of the action
var Logger = System.getModule("com.simplygeek.library.util").logger(logType, logName);
var log = new Logger(logType, logName);
var vcVm;
var vcVmName = "";
var vCenterSdkName;
var searchInVcDataCenter = null; // Set to vCDatacenter object to limit search or Null to search entire inventory.
var searchForVms = true; // false will search for hosts.
var searchByInstanceUuid = true; // false will search by BIOS UUID.

try {
    checkParams(vCenterSDK, vcVmInstanceUuid);
    vCenterSdkName = vCenterSDK.name;
    log.log("Attempting to locate vcenter vm with instance uuid '" + vcVmInstanceUuid +
            "' on vCenter '" + vCenterSdkName + "'");
    vcVm = vCenterSDK.searchIndex.findByUuid(searchInVcDataCenter, vcVmInstanceUuid,
                                             searchForVms, searchByInstanceUuid);
    if (vcVm) {
        vcVmName = vcVm.name;
        log.log("Found vcenter virtual machine '" + vcVmName + "'");
    } else {
        log.error("The vcenter virtual machine could not be found with instance uuid '" + vcVmInstanceUuid + "'");
    }
} catch (e) {
    log.error("Action failed to locate vCenter virtual machine.",e);
}

return vcVm;
[2019-01-30 15:25:57.623] [I] [Action: getVcenterSdkBySiteId] Locating vCenter Server with site id: SG1
[2019-01-30 15:25:57.635] [I] [Action: getVcenterSdkBySiteId] Found vCenter Server 'https://sg1-vsa001.sgroot.local:443/sdk'
[2019-01-30 15:25:57.668] [I] [Action: findVcVmByInstanceUuidOnVcenterSdk] Attempting to locate vcenter vm with instance uuid '5037893e-5ea4-a051-5725-32a4664aad67' on vCenter 'https://sg1-vsa001.sgroot.local:443/sdk'
[2019-01-30 15:25:57.682] [I] [Action: findVcVmByInstanceUuidOnVcenterSdk] Found vcenter virtual machine 'SG1-VRA001'

Find the vCenter VM by its name, on a specific vCenter Server and a specific folder

I had a requirement to search for a vCenter VM on the same vCenter Server when performing same site recoveries with EMC backups or Zerto. The issue I had was that there would be an existing VM with the same instance uuid and even the same hostname, but the VMs would be located in different folders (the target folder and the temporary recovery folder). Once a restore had been completed, I needed to move the VM from the temporary recovery folder to the target folder and also run some additional workflows to restore tags, custom attributes, etc.

This action searches by the VM name, which is safe enough when targeting a specific folder. This helps locate the recovered VM that might have been restored with a different instance uuid. You could also modify this Action to search by the instance uuid if you so wish by adapting code from the previous sections.

Note that you can get the code to discover the vCenterSdk in the previous section ‘Locate the vCenter VM by its instance Uuid on a specific vCenter Server’, above.

findVcVmByInstanceUuidOnVcenterSdkInFolder

/*global vCenterSdk vmName folderName*/

/**
 * Searches for a vCenter VM by its name on the specified vCenter Server and folder name.
 * @author Gavin Stephens <gavin.stephens@simplygeek.co.uk>
 * @version 1.1.0
 * @function findVcVmByNameOnVcenterSdkInFolder
 * @param {VC:SdkConnection} vCenterSdk - The vCenter Server SDK Connection.
 * @param {string} vmName - The virtual machine name.
 * @param {string} folderName - The vCenter virtual machine folder name.
 * @returns {VC:VirtualMachine} returns the vCenter virtual machine object.
 */

function checkParams(vCenterSdk, vmName, folderName) {
    var inputErrors = [];
    var errorMessage;
    if (!vCenterSdk || System.getObjectType(vCenterSdk) !== "VC:SdkConnection") {
        inputErrors.push(" - vCenterSdk missing or not of type 'VC:SdkConnection'");
    }
    if (!vmName || typeof vmName !== "string") {
        inputErrors.push(" - vmName missing or not of type 'string'");
    }
    if (!folderName || typeof folderName !== "string") {
        inputErrors.push(" - folderName missing or not of type 'string'");
    }
    if (inputErrors.length > 0) {
        errorMessage = "Mandatory parameters not satisfied:\n" + inputErrors.join("\n");
        log.error(errorMessage);
    }
}

var logType = "Action";
var logName = "findVcVmByNameOnVcenterSdkInFolder"; // This must be set to the name of the action
var Logger = System.getModule("com.simplygeek.library.util").logger(logType, logName);
var log = new Logger(logType, logName);
var vcVms = [];
var vcVmsInFolder = [];
var vcVm;
var numVcVms = 0;
var vcVmName = "";
var xPath = "";
var vcVmInstanceUuid = "";

try {
    checkParams(vCenterSdk, vmName, folderName);
    xPath = "xpath:name='" + vmName + "'";
    log.log("Attempting to locate vCenter vm '" + vmName + "' in folder '" + folderName + "'");
    vcVms = vCenterSdk.getAllVirtualMachines(null, xPath);
    numVcVms = vcVms.length;
    log.log("Found " + numVcVms + " vCenter virtual machine(s).");
    if (numVcVms > 0) {
        vcVmsInFolder = vcVms.filter(function(x){return x.parent.name.toLowerCase() === folderName.toLowerCase();});
        if (vcVmsInFolder.length > 0) {
            vcVm = vcVmsInFolder[0];
            vcVmName = vcVm.name;
            vcVmInstanceUuid = vcVm.config.instanceUuid;
            log.log("Found vCenter virtual machine '" + vcVmName + "' with instance uuid '" + vcVmInstanceUuid + "'");
        }
    }
    if (!vcVm) {
        log.error("No vCenter vm was found with the name '" + vmName + "' in folder '" + folderName + "'");
    }
} catch (e) {
    log.error("Action failed to locate vCenter vm.",e);
}

return vcVm;
[2019-01-30 15:39:33.948] [I] [Action: getVcenterSdkBySiteId] Locating vCenter Server with site id: SG1
[2019-01-30 15:39:33.957] [I] [Action: getVcenterSdkBySiteId] Found vCenter Server 'https://sg1-vsa001.sgroot.local:443/sdk'
[2019-01-30 15:39:33.994] [I] [Action: findVcVmByInstanceUuidOnVcenterSdkInFolder] Attempting to locate vCenter vm 'SG1-VRA001' in folder 'CMP'
[2019-01-30 15:39:34.028] [I] [Action: findVcVmByInstanceUuidOnVcenterSdkInFolder] Found 1 vCenter virtual machine(s).
[2019-01-30 15:39:34.044] [I] [Action: findVcVmByInstanceUuidOnVcenterSdkInFolder] Found vCenter virtual machine 'SG1-VRA001' with instance uuid '5037893e-5ea4-a051-5725-32a4664aad67'

Find the vCenter VM by its MoRef Id on a specific vCenter Server

I had yet another requirement to search for a vCenter VM by its MoRef ID. This was because I was working with Zerto, which stores the MoRef Id as part of the Zerto vm id. I needed to be able to discover the vCenter VM using this MoRef.

Note that you can get the code to discover the vCenterSdk in the previous section ‘Locate the vCenter VM by its instance Uuid on a specific vCenter Server’, above.

findVcVmByMorefIdOnVcenterSdk

/*global vCenterSdk vcVmMoRefId*/

/**
 * Searches for a vCenter VM by its Managed Object Reference Id on the specified vCenter Server.
 * @author Gavin Stephens <gavin.stephens@simplygeek.co.uk>
 * @version 1.1.0
 * @function findVcVmByMorefIdOnVcenterSdk
 * @param {VC:SdkConnection} vCenterSdk - The vCenter Server SDK Connection.
 * @param {string} vcVmMoRefId - The vCenter VM Managed Object Reference Id.
 * @returns {VC:VirtualMachine} returns the vCenter virtual machine object.
 */

function checkParams(vCenterSdk, vcVmMoRefId) {
    var inputErrors = [];
    var errorMessage;
    if (!vCenterSdk || System.getObjectType(vCenterSdk) !== "VC:SdkConnection") {
        inputErrors.push(" - vCenterSdk missing or not of type 'VC:SdkConnection'");
    }
    if (!vcVmMoRefId || typeof vcVmMoRefId !== "string") {
        inputErrors.push(" - vcVmMoRefId missing or not of type 'string'");
    }
    if (inputErrors.length > 0) {
        errorMessage = "Mandatory parameters not satisfied:\n" + inputErrors.join("\n");
        log.error(errorMessage);
    }
}

var logType = "Action";
var logName = "findVcVmByMorefIdOnVcenterSdk"; // This must be set to the name of the action
var Logger = System.getModule("com.simplygeek.library.util").logger(logType, logName);
var log = new Logger(logType, logName);
var vcVmName = "";
var numVcVms = 0;
var vcVm;
var vcVms = [];
var xPath = "";

try {
    checkParams(vCenterSdk, vcVmMoRefId);
    log.log("Attempting to find vcenter vm with moref id '" + vcVmMoRefId + "'");
    xPath = "xpath:id='" + vcVmMoRefId + "'";
    vcVms = vCenterSdk.getAllVirtualMachines(null, xPath);
    numVcVms = vcVms.length;
    if (numVcVms > 1) {
        log.error("More than one vCenter VM found. I don't know which one you're looking for!");
    } else if (numVcVms > 0) {
        vcVm = vcVms[0];
        vcVmName = vcVm.name;
        log.log("Found vCenter virtual machine '" + vcVmName + "'");
    } else {
        log.error("No vCenter vm was found with MoRef Id '" + vcVmMoRefId + "'");
    }
} catch (e) {
    log.error("Action failed to find vCenter VM by MoRef Id.",e);
}

return vcVm;
[2019-01-30 16:42:24.130] [I] [Action: getVcenterSdkBySiteId] Locating vCenter Server with site id: SG1
[2019-01-30 16:42:24.142] [I] [Action: getVcenterSdkBySiteId] Found vCenter Server 'https://sg1-vsa001.sgroot.local:443/sdk'
[2019-01-30 16:42:24.176] [I] [Action: findVcVmByMorefIdOnVcenterSdk] Attempting to find vcenter vm with moref id 'vm-49'
[2019-01-30 16:42:24.203] [I] [Action: findVcVmByMorefIdOnVcenterSdk] Found vCenter virtual machine 'SG1-VRM001'

That was a lot of content and I hope you find useful. A link to the package containing all the code and workflows is included at the beginning of this post. As always, please contact me through Drift and I’ll do what I can to help!

Please rate this post!

Average rating / 5. Vote count:

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Rama Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Rama
Guest
Rama

You are the best.. thank you for your great work and sharing.