Search tricks in vRO

One of my customers asked me what is the most efficient way to find VM’s in vCenter based on UUID/moRef/IP/Name with vRO… Well… this is a good question! and this post will show you how you can do it.

The most inefficient way to find VM by IP/Name/ID is like this:

  1. vms = VcPlugin.getAllVirtualMachines();
  2.  
  3. for each (vm in vms) {
  4.     if (vm.guest.ipAddress == ip) {
  5.         System.log(“VM name: “+vm.name);
  6.     }
  7. }

 

In large environments this could take some time.

So first, I want to show you how you can do it from the vCenter MOB (Managed Object Browser). What? MOB?

The Managed Object Browser, or MOB, is a Web-based server application available for all ESX/ESXi and vCenter Server systems. The MOB lets you examine the objects that exist on the server and navigate through the hierarchy of live objects by clicking on links. The MOB populates the browser with actual runtime information, for example, the names of properties.

In order to use the MOB interface browse to your vCenter (example: https://vcenter01.corp.local) and press “Browse objects managed by vSphere”, then you will need to enter the credentials to the vCenter.

Screenshot 2015-06-28 10.57.59

OK! We are in! what can we do? well… lots of stuff. but let us focus in the search options.

1. Press “content”

Screenshot 2015-06-28 11.02.06

2. You will see lots of “Object Managers” that can help you to execute different queries and methods against vCenter. We will use the “SearchIndex” entry.

Screenshot 2015-06-28 11.22.50

 

3. And here you go… small and useful vCenter queries! let’s use the “FindByIp”. Once you will press it, you will get a popup like this:

Screenshot 2015-06-28 11.26.53

There are 3 fields:

  1. Datacenter (Optional): The MOID of the Datacenter (I’ll show you how to get it)
  2. IP (required)
  3. vmSearch (required): Yes, you can search not only VM’s! This is boolean field, “true”/”false”

OK, first let’s see how how we extract the Datacenter MOID. Go back to the “Object Managers” page, and enter to the “rootFolder”, which is basically the inventory that you can browse.

Screenshot 2015-06-28 11.39.32

You will see in the top of the page the datacenter ID:

Screenshot 2015-06-28 11.41.25

Cool! copy the ID and switch back to the “findByIp” tab, and replace this “MOID” with the ID of the datacenter, should be like this:

  1. <!– optional –>
  2. <datacenter type=“Datacenter”>datacenter-21</datacenter>

 

And fill the rest as I did in the example and press “Invoke Method”:

Screenshot 2015-06-28 11.46.56

ammm OK, but how this is related to vRO? check this out 🙂

Screenshot 2015-06-28 11.49.08

vRO has scripting class for “SearchIndex” exactly like you saw in MOB. “SearchIndex” scripting class is a child object of VC:SDKConnection that basically has the same structure and methods as the MOB.

Let’s test it, create new workflow with this Scriptable Task (Action will be better way to do it):

Screenshot 2015-06-28 11.55.51

The Code:

  1. object = sdkConneciton.searchIndex.findByIp(datacenter, ip, vm);
  2. System.log(“Object Name: “+object.name);

 

Input Parameters:

  1. sdkConnection – VC:sdkConnection (the vCenter Connection)
  2. ip – String (you can create IP address validation using the presentation)
  3. vm – boolean, in order search VM’s or other objects
  4. datacenter – VC:Datacenter object (optional)

This is far better and faster way than iterate the entire vCenter VM’s array!

If you want to use the “findByDnsName” method in order to find VM’s, make sure that VMware Tools is running.

If VMware Tools isn’t an option, you can use the OOTB workflow “Get virutal machines by name”. The only disadvantage of this workflow, is that the output parameter is array of VM’s, so you can take the code and adjust it so it will fit your own needs.

 

And this is it, I hope this post will help to improve the performance of your workflows.

Please feel free to leave comments.

 

2 thoughts on “Search tricks in vRO

Leave a Reply