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
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);
}
}
Something like that will be a lot more effective:
var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[translate(.,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + arg_nameToMatch.toUpperCase() + "']");
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)) {
...
}
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
Yes I tried that, but it does not work.
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.
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.
Ilian Iliev wrote:
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.
Could you please try to replace "B00019V" with a variable?
I think that might be the difference.
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);
}
}
Thank you. That works perfectly.
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
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