I found sample code in the communities, but it appears that the searches are case sensitive. We need help modifying the script to make it case insensitive.
var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name='"+vmName+"'");
var vmObject = null;
if (vms != null && vms.length >0){
System.log("VMs Found: " + vms.length);
if (vms.length == 1){
System.log("Match found for vm named: "+vmName);
vmObject = vms[0];
}else{
System.log("More than one VM found with that name! "+vmName);
for each (vm in vms){
System.log("VM ID: "+vm.id);
}
}
}else{
System.log("No VMs Found");
}
.
Hi, I was about to post a different question about this same method. Here's what I use to do case-insensitive search:
var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[translate(.,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + arg_nameToMatch.toUpperCase() + "']");
Hi,
the xpath "matches()" method might help. Find an example here:
xml - case-insensitive matching in xpath? - Stack Overflow
Cheers,
Joerg
hm, the xpath in vCO doesn't seem to support matches(.,'vmname',"i") with the flag "i" for case insensitive 😞
Can you use the library Action com.vmware.library.vc.vm.getAllVMsMatchingRegexp instead? with /vmname/i as Regexp parameter?
Cheers,
Joerg
Hi, I was about to post a different question about this same method. Here's what I use to do case-insensitive search:
var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[translate(.,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + arg_nameToMatch.toUpperCase() + "']");
Wow, that rocks! Thanks for sharing!
Not really an intuitive solution, but it indeed works...
Cheers,
Joerg
PS.: I felt free to mark this as the correct answer
That works great. Thanks!!
Hi,
I followed your solution and want to search the VM name in vCD, I am providing the virtual machine name as a string (input) and I want the script to return the VM as vCloud:VM.(output)
Please help me.
I'm using vRO 7.0.1 and had success doing a case insensitive search for VM name by modifying the builtin "Get virtual machines by name" workflow to the following syntax:
var sdkConnections = VcPlugin.allSdkConnections;
vms = new Array();
for (var i in sdkConnections) {
var host = sdkConnections[i];
var found = host.getAllVirtualMachines(null, "xpath:matches(name, '(?i)" + criteria + "')");
for (var j in found) {
vms.push(found[j]);
}
}