VMware Cloud Community
Mnemonic
Enthusiast
Enthusiast
Jump to solution

Find VM by Name RegEx - How to ignore case

Hi,

In vCO there is a standard Action that looks like this:

----------

// Get all Virtual Machines for all vCenter connections defined for this plugin

var allVms = VcPlugin.getAllVirtualMachines();

var vms = new Array();

// Check if the VM match the regexp

for (var i in allVms) {

  if (allVms[i].name.match(regexp)) {

  vms.push(allVms[i]);

  }

}

VMList = vms;

----------

How can I make this actions ignore the case. I searched for this in Java script, and found that I should add "/i" to the regexp, but I cannot get it to work.

  if (allVms[i].name.match(regexp/i)) {

Does anyone know how to get this working?

/Brian

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

This should work with a variable:

var allDcs = VcPlugin.allDatacenters;

var nameToMatch = "B00019V";

for (var i in allDcs) {

  if (allDcs[i].name.match(new RegExp(nameToMatch, "i"))) {

    System.log("found match -> " + allDcs[i].name);

  }

}

View solution in original post

0 Kudos
10 Replies
cdecanini_
VMware Employee
VMware Employee
Jump to solution

Something like that will be a lot more effective:

var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[translate(.,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + arg_nameToMatch.toUpperCase() + "']");

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Brian,

Does your regexp start with '/' character? For example, to match VM names which contain 'vco' (case-insensitively), something like this should work

if (allVms[i].name.match(/vco/i)) {

   ...

}


0 Kudos
Mnemonic
Enthusiast
Enthusiast
Jump to solution

Ilian Iliev wrote:

Brian,

Does your regexp start with '/' character? For example, to match VM names which contain 'vco' (case-insensitively), something like this should work

  1. if (allVms[i].name.match(/vco/i)) { 
  2.    ... 
  3. }


Yes I tried that, but it does not work.

0 Kudos
Mnemonic
Enthusiast
Enthusiast
Jump to solution

cdecanini_ wrote:

Something like that will be a lot more effective:

var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[translate(.,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + arg_nameToMatch.toUpperCase() + "']");

Yes that seems more effective, but I cannot get it to work.

My script now looks like this

var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[translate(.,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + regexp.toUpperCase() + "']");

VMList = vms;

---

The value of my regexp is B00019V.

It seems to only work when I type the exact name of a VM.

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Tried the following script in my environment:

var allDcs = VcPlugin.allDatacenters;

for (var i in allDcs) {

  if (allDcs[i].name.match(/B00019V/i)) {

    System.log("found match -> " + allDcs[i].name);

  }

}

and it does work as expected.

0 Kudos
Mnemonic
Enthusiast
Enthusiast
Jump to solution

Ilian Iliev wrote:

Tried the following script in my environment:

  1. var allDcs = VcPlugin.allDatacenters; 
  2. for (var i in allDcs) { 
  3.   if (allDcs[i].name.match(/B00019V/i)) { 
  4.     System.log("found match -> " + allDcs[i].name); 
  5.   } 

and it does work as expected.

Could you please try to replace "B00019V" with a variable?

I think that might be the difference.

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

This should work with a variable:

var allDcs = VcPlugin.allDatacenters;

var nameToMatch = "B00019V";

for (var i in allDcs) {

  if (allDcs[i].name.match(new RegExp(nameToMatch, "i"))) {

    System.log("found match -> " + allDcs[i].name);

  }

}

0 Kudos
Mnemonic
Enthusiast
Enthusiast
Jump to solution

Thank you. That works perfectly.

0 Kudos
GreenDotCloudAd
Contributor
Contributor
Jump to solution

cdecanini_

This worked for me perfectly. Instead of the getAllVirtualMachines method returning all the VM's and then my iterating throught all of them it only returns the VM's that match my criteria. So in my case I'm looking for a specific VM name.

My action code is below. I'm only passing the virtual machine name (vmName) as an input variable.

//Action Object to retrieve the IP Address from a VM using the VM Tools information
System.log("*****************************************************************************");
var actionName = arguments.callee.name.substr(6); 
System.log("Action Start: " + actionName);

var retIPAddress = "";

var xPathQuery = "xpath:name[translate(.,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + vmName.toUpperCase() + "']";
System.debug("Query: " + xPathQuery);
System.debug("VM Name : " + vmName);


var allVMs = VcPlugin.getAllVirtualMachines(null, xPathQuery);

System.debug("Number of VMs found: " + allVMs.length);

//Loop through all VM's to find the VM that matches the name parameter
for (var i in allVMs){
if (allVMs[i].name.toLowerCase() == vmName.toLowerCase()){
  var guestInfo = allVMs[i].guest;
  retIPAddress = guestInfo.ipAddress;
  System.debug(allVMs[i].name + " found!");
  System.log("IP Address: " + retIPAddress);
}
}

System.log("Action Finish: " + actionName);

return retIPAddress;

Luis Bonilla

Sr. Systems Engineer

Green Dot Corporation

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

Anyone following this thread would probably be interested in what I have found with the xpath searches.  Seems to be some differences in how the strings are interpreted.  I'm not sure if it is Orchestrator or vSphere plugin that is the culprit.  I do know that the translate function seems to work differently.  In earlier versions I didn't need to include translate for numbers and dashes if those were there they would just not get translated.  Now translate is working like I see it documented in xpath and characters get dropped if they aren't in the translation table.  So for the below examples "0123456789-." need to be added.  I also found I needed to escape out the single quotes where I didn't need to before.  See this post xpath searches in the vCenter plugin for exact match and case insensitivity

0 Kudos