I couldn't find an action for this and it doesn't seem to be a property of the vCAC vm. It sure would be nice if it were. I snagged this code from one of the workflows in the vCAC plugin that does what I would like so I figured I would repost here. I also attached an action. Takes a vCAC:VirtualMachine input and returns a VC:VirtualMachine out.
var sdkConnections = VcPlugin.allSdkConnections;
System.log(sdkConnections.length + " sdk Connections found...");
for each (var sdkConnection in sdkConnections) {
try {
vCenterVm = sdkConnection.searchIndex.findByUuid(null, vCACVm.vmUniqueID, true, false);
} catch(e) {System.log("Error for SDK connection " + sdkConnection.name + " : " + e);}
if (vCenterVm != null) {
System.log("Resolved vCenter VM " + vCenterVm.name);
return vCenterVm;
break;
}
}
Nice work Paul -- that is a nearly identical line by line action code to what I wrote in August 2013 for a POC I worked on ....
Heh... it probably is your code. Like I said I stole it and repackaged it! :smileysilly:
My order of preference :
BTW the extensibility package (which is now included in the vCAC plug-in) does include code to do the vCAC VM to vCenter VM (and also from a vCloud vApp).
That is where I stole it (the vCAC plugin). It sure would be nice if it was a supported action in the library. Better yet just have a couple of properties on the vCAC:VirtualMachine object itself. Hopefully it isn't there and I just missed it. I can't remember which workflow I snagged it out of. One of the helpers it was one of the scriptable tasks.
Since it is part of the plug-in, it IS supported
Does anyone have a similar solution solution to this but for the other way round i.e. takes a vCenter VM and returns a vCAC VM?
Up until now I've used getUnmanagedVirtualMachines(host) and cycled through comparing the uuid, is there no way you can get all VMs/managed VMs?
This is the way I am getting the vCAC:VirtualMachine. Not very elegant but it works. It is basically doing what you are.
var vCACVms = Server.findAllForType("vCAC:VirtualMachine");
var uuid = vCenterVm.config.uuid;
for each (var vCACVm in vCACVms) {
if (uuid == vCACVm.vmUniqueID) {
System.log("Resolved vCAC VM: " + vCACVm.virtualMachineName);
return vCACVm;
}
}
I figure I might as well post some other helpful snippets:
Get a vCAC:VirtualMachine by name -
var vCACVms = Server.findAllForType("vCAC:VirtualMachine", "VirtualMachineName eq '" + vCACVmName + "'");
for each (var vm in vCACVms) {
System.log(vm.virtualMachineName);
}
return vCACVms;
Get the vCAC:VCACHost from a vCAC:VirtualMachine -
if(vCACVm != null){
var host = Server.findForType("vCAC:VCACHost", vCACVm.getEntity().hostId);
if(host) return host;
}
I know this has some time, but here´s another way. I used the getAllVMsMatchingRegexp vCO action as a base:
virtualMachine is a VCAC:VirtualMachine
vm is a VC:VirtualMachine
// Get vCenter VM ID from vCAC VM
var vmid = virtualMachine.externalReferenceId;
// Get all Virtual Machines for all vCenter connections defined for this plugin
var allVms = VcPlugin.getAllVirtualMachines();
// Check if the VM match the id
for (var i in allVms) {
if (allVms[i].id.match(vmid)) {
vm = allVms[i];
break;
}
}
[Moderator note: The author of this post is from DoubleCloud. The FTC’s Endorsement Guides require disclosure of vested interests in social media posts.]
Using DoubleCloud vSearch is the easiest way with Google like search, see the following screenshot. You can find any VMs from any connected vCenter by their name, ip address, MAC address, etc. Once you can the VM, you can access it directly using HTML5 SSH client inside browser, and manage the VM in a light speed.
If you guys have found this useful "like" my post. Trying to level up on here!
Just to show there are many methods of the same thing. Here's the version I wrote that utilises an xpath lookup using the vSphere MoRef.
// Grab the vSphere MoRef from vRA Object
var externalReferenceId = vcacVm.externalReferenceId;
// Construct xPath
var xpath = "xpath:id='" +externalReferenceId+ "'";
// Utilise the VCPlugin and return first vm in the array.
var vm = VcPlugin.getAllVirtualMachines(null ,xpath)[0];
if(vm.length == 0){
System.debug("Couldn't find corresponding VC:Virtualmachine for " + vcacVm.displayName);
return null;
}
System.log("Found " + vcacVm.displayName + " in VC:VirtualMachine");
return vm;
Here is yet another way... apparently I changed my methodology at some point. I think this search was much faster on a really large inventory with multiple vCenters. Anyway this has been working great for me.
var sdkConnections = VcPlugin.allSdkConnections;
var vCenterVm = null;
for each (var sdkConnection in sdkConnections) {
try {
vCenterVm = sdkConnection.searchIndex.findByUuid(null, vCACVm.vmUniqueID, true, true);
} catch(e) {
System.log("Unable to find VM by instance UUID :"+vCACVm.vmUniqueID+", for SDK connection " + sdkConnection.name + ", will fallback to search by BIOS UUID, reason : " + e);
try {
vCenterVm = sdkConnection.searchIndex.findByUuid(null, vCACVm.vmUniqueID, true, false);
} catch(e) {
System.log("Unable to find VM by BIOS UUID :"+vCACVm.vmUniqueID+", for SDK connection " + sdkConnection.name + ", reason : " + e);
}
}
if (vCenterVm != null) {
System.log("Resolved vCenter VM " + vCenterVm.name);
break;
} else{
try {
vCenterVm = sdkConnection.searchIndex.findByUuid(null, vCACVm.vmUniqueID, true, false);
} catch(e) {
System.log("Unable to find VM by BIOS UUID :"+vCACVm.vmUniqueID+", for SDK connection " + sdkConnection.name + ", reason : " + e);
}
}
if (vCenterVm != null) {
System.log("Resolved vCenter VM " + vCenterVm.name);
break;
}
}
return vCenterVm;
Any idea what an average runtime can be expected for a single vCenter connection and an inventory of ~200 VMs? I'm finding it's taking quite a while and I'm not sure if I'm missing something.
How long is long? I just tested on my production instance which has 7 vCenters and 10K plus vm's and I pretty much got an immediate response.
What is the difference of using `sdkConnection.searchIndex.findByUuid` instead of `Server.findForType( 'Vc:VirtualMachine', vmUniqueID )` or `Server.findAllForType( 'Vc:VirtualMachine', vmUniqueId )`? Just curious as I use the Server class methods, but am not sure if there is any benefit to using one or the other.
One search is using vCenter to execute a search and the other is searching the vRO inventory. I think in my vRO instances with large environments the vCenter sdkConnection searches were much faster.
Hello Pezh0re,
I was facing same issue as you. I have more than 1K VMs on 4 vCenter and when I use "sdkConnection.searchIndex", it's, first thing, take a really long time (15 min) and second thing, there is like a leak memory issue on VRO Java process, and I need to restart server after 10 days...
So, I never use this method anymore, I use a containerView like explained in this thread getAllVMsOfCluster slowness vs Get-VM .
From 15min, it's now 150ms and Java Process Memory is OK.
The "containerView method" is also used in a workflow included in vCenter Plugin (Library / vCenter / Property collector / get virtual machines by name with PC).
Emmanuel.