10 Replies Latest reply on Mar 14, 2017 10:09 AM by qc4vmware

    Find VM by Name RegEx - How to ignore case

    Brian Knutsson Enthusiast
    vExpert

      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

        • 1. Re: Find VM by Name RegEx - How to ignore case
          cdecanini_ Virtuoso
          VMware Employees

          Something like that will be a lot more effective:

           

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

          • 2. Re: Find VM by Name RegEx - How to ignore case
            iiliev Champion
            Community WarriorsVMware Employees

            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)) {
               ...
            
            }
            


            • 3. Re: Find VM by Name RegEx - How to ignore case
              Brian Knutsson Enthusiast
              vExpert

              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.

              • 4. Re: Find VM by Name RegEx - How to ignore case
                Brian Knutsson Enthusiast
                vExpert

                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.

                • 5. Re: Find VM by Name RegEx - How to ignore case
                  iiliev Champion
                  Community WarriorsVMware Employees

                  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.

                  • 6. Re: Find VM by Name RegEx - How to ignore case
                    Brian Knutsson Enthusiast
                    vExpert

                    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.

                    • 7. Re: Find VM by Name RegEx - How to ignore case
                      iiliev Champion
                      Community WarriorsVMware Employees

                      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);
                        }
                      }
                      
                      • 8. Re: Find VM by Name RegEx - How to ignore case
                        Brian Knutsson Enthusiast
                        vExpert

                        Thank you. That works perfectly.

                        • 9. Re: Find VM by Name RegEx - How to ignore case
                          GreenDotCloudAdmin Lurker

                          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

                          • 10. Re: Find VM by Name RegEx - How to ignore case
                            qc4vmware Master

                            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