VMware Cloud Community
sushantghige
Contributor
Contributor

How to find out Datacenter name using only VMName?

I am quite new to vRO and developing a automation where I need to find datacenter using only VMName.

I am able to do it using, iterating all the datacenter and looking for my VMName.

var myDatacenters = VcPlugin.getAllDatacenters();

System.log(myDatacenters.length);

var bFound = false;

for each (dc in myDatacenters)

{

var allVMs = System.getModule('com.vmware.library.vc.datacenter').getAllVMsOfDatacenter(dc);

for each (vm in allVMs)

{

if (vm.name == vmname)

{

DataCenterName = dc.name;

System.log("Found the datacenter for VM "+vmname+" : "+DataCenterName);

return DataCenterName;

}

}//innerfor

}//outerfor

 

But this doesn't seem to be efficient way as number of VMs will grwo and this lookup will become lot slower.

Can somebody help me with better approach to find the Datacenter name using VmName?

 

thanks,

Sushant

var myDatacenters = VcPlugin.getAllDatacenters();
    System.log(myDatacenters.length);
    var bFound = false;
    for each (dc in myDatacenters)
    {
        var allVMs = System.getModule('com.vmware.library.vc.datacenter').getAllVMsOfDatacenter(dc);
        for each (vm in allVMs)
        {
            //System.log(vm.name);
            if (vm.name == vmname)
            {
                DataCenterName = dc.name;
                System.log("Found the datacenter for VM "+vmname+" : "+DataCenterName);
                //DataCenterName="gtdc";
                return DataCenterName;
            }
        }//innerfor
    }//outerfor
Reply
0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee

Hi Sushant,

Check the following sample code:

var vmName = "myvmname"; // replace with your VM name

var vms = Server.findAllForType("VC:VirtualMachine", "xpath:name='" + vmName + "'");

if (vms == null || vms.length == 0) {

  System.log("No virtual machine with such name found");

} else {

  if (vms.length > 1) {

    System.log("More than one virtual machine with such name found; will use the first one");

  }

  var vm = vms[0];

  var vmparent = vm.parent;

  while (vmparent != null && !(vmparent instanceof VcDatacenter)) {

    vmparent = vmparent.parent;

  }

  if (vmparent == null) {

    System.log("Parent datacenter not found.");

  } else {

    System.log("Parent datacenter name: " + vmparent.name);

  }

}

What the code does is pretty straightforward. First it retrieves a VC:VirtualMachine object for the given machine name, and then it iterates over virtual machine parents chain until it finds parent of type VC:Datacenter.

The main problem is that the virtual machine name is not unique; eg. you can have several virtual machines named 'MyVM' in different folders. The sample code above detects this situation and simply picks the first matching virtual machine from the list. In production code, you may want to use a different strategy to handle non-unique names.

Reply
0 Kudos
sushantghige
Contributor
Contributor

Hi,

Thank you for the valuable input. This was helpful. 

For me the VM Names are unique across all datacenters so the mentioned problem is not going to trouble ( at least not till we decide to go against unique naming).

Thanks again ....

Reply
0 Kudos