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:
- vms = VcPlugin.getAllVirtualMachines();
- for each (vm in vms) {
- if (vm.guest.ipAddress == ip) {
- System.log(“VM name: “+vm.name);
- }
- }
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.
OK! We are in! what can we do? well… lots of stuff. but let us focus in the search options.
1. Press “content”
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.
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:
There are 3 fields:
- Datacenter (Optional): The MOID of the Datacenter (I’ll show you how to get it)
- IP (required)
- 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.
You will see in the top of the page the datacenter ID:
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:
- <!– optional –>
- <datacenter type=“Datacenter”>datacenter-21</datacenter>
And fill the rest as I did in the example and press “Invoke Method”:
ammm OK, but how this is related to vRO? check this out 🙂
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):
The Code:
- object = sdkConneciton.searchIndex.findByIp(datacenter, ip, vm);
- System.log(“Object Name: “+object.name);
Input Parameters:
- sdkConnection – VC:sdkConnection (the vCenter Connection)
- ip – String (you can create IP address validation using the presentation)
- vm – boolean, in order search VM’s or other objects
- 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.
Great Article, helped me out loads.
nice article.. easy for learning purpose