VMware Cloud Community
notorious_bdg
Enthusiast
Enthusiast
Jump to solution

Case insensitive search for VM by name

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");

}

.

0 Kudos
1 Solution

Accepted Solutions
robrtb12
Enthusiast
Enthusiast
Jump to solution

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() + "']");

View solution in original post

0 Kudos
7 Replies
tschoergez
Leadership
Leadership
Jump to solution

Hi,

the xpath "matches()" method might help. Find an example here:

xml - case-insensitive matching in xpath? - Stack Overflow

Cheers,

Joerg

0 Kudos
tschoergez
Leadership
Leadership
Jump to solution

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

0 Kudos
robrtb12
Enthusiast
Enthusiast
Jump to solution

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() + "']");

0 Kudos
tschoergez
Leadership
Leadership
Jump to solution

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

0 Kudos
notorious_bdg
Enthusiast
Enthusiast
Jump to solution

That works great.  Thanks!!

0 Kudos
Nainz
Contributor
Contributor
Jump to solution

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.

0 Kudos
FidelisCare
Contributor
Contributor
Jump to solution

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]);

  }

}