Skip to content

Instantly share code, notes, and snippets.

@imtrinity94
Forked from vmwarecode/snippet.js
Created May 11, 2023 16:39
Show Gist options
  • Save imtrinity94/f658e579f45d4989f2950c287ea7de72 to your computer and use it in GitHub Desktop.
Save imtrinity94/f658e579f45d4989f2950c287ea7de72 to your computer and use it in GitHub Desktop.
Find vSphere VM by Name across all vCenters in vRealize Orchestrator
//action Inputs:
// vmName - string
//
//Return Type: VC:VirtualMachine
var found = VcPlugin.getAllVirtualMachines(null, "xpath:name[matches(.,'"+vmName+"')]");
if (found.length > 1) {
throw(vmName+" matched more than one Virtual Machine");
}
if (found.length == 1) {
return found[0];
}
throw("Virtual Machine does not exist by name: "+vmName);
@imtrinity94
Copy link
Author

// Input is vm_name (string), output is vm_object (vc:virtualmachine)
// Needs to find only 1 VM matching the name in the inventory
// This version skips powered off VM’s (SRM placeholders)

var qry = "xpath:name='" + vm_name + "'";
var vms_found = Server.findAllForType("VC:VirtualMachine", qry);
var good_vms = [];

System.log("Number of VM's found: " + vms_found.length);
for(var i = 0; i < vms_found.length; i++){
System.log("vCenter is " + vms_found[i].parent.parent);
if(vms_found[i].state == "poweredOn"){
System.log("Status is " + vms_found[i].state) + " adding to good vm's array.";
good_vms.push(vms_found[i]);
}
}

if(good_vms.length == 1){
vm_object = good_vms[0];
} else {
throw("Number of powered on VM's found with the name " + vm_name + " is not 1. Exiting!");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment