VMware Cloud Community
jonathanvh
Enthusiast
Enthusiast
Jump to solution

Return cluster of VM

I have a simple question, but I don't seem to find an answer.

A workflow returns the object: VcVirtualMachine

But I can't find a way to get the cluster of the VM.

Jonathan

1 Solution

Accepted Solutions
leefriendoaklan
Contributor
Contributor
Jump to solution

Hi Jonathanvh,

Looking at this, you may get better performance if you go through the host to get the cluster. Just a thought as a large inventory may take a while to loop through. Try this...

var cluster = ((vm.runtime).host).parent;

OR (if you prefer easier to read code)

var vcVirtualMachineRuntimeInfo = vm.runtime;

var hostSystem = vcVirtualMachineRuntimeInfo.host;

var cluster = hostSystem.parent;

I would suggest that you check the result with something like...

if(cluster.vimType == "ClusterComputeResource"){

    System.log(cluster.name);

}

else

{

    System.log("Standalone_host");

}

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thanks.

View solution in original post

3 Replies
Manupa
Enthusiast
Enthusiast
Jump to solution

hi,

you can get the name of you vm by using vm.name, what you get is a string.

then can use this script:

var allVMs = VcPlugin.getAllVirtualMachines();

var Cluster = VcPlugin.allClusterComputeResources;

var vms = null;

     for(i in Cluster.host)

            {

                 for (j in Cluster.host[i].vm)

                      if(vmname == Cluster.host[i].vm[j].name)

                           System.log("Found VM on Cluster: " + Cluster.host[i].name);

                           break;

            }

Or you use your object and its parent.

System.log(virtualmachine.parent.parent.name);

returns the Cluster, if you do not use folders.

First parent is the host, the vm is running on, secon is the cluster. folder would also be a parent.

hope this helps

Manuel

jonathanvh
Enthusiast
Enthusiast
Jump to solution

Hi Manupa,

We are in fact using folders.

But your answer gave me an idea.

And this is the solution I found: System.log ("VM Cluster: " + vm.resourcePool.parent.parent.name);

Thanks!

Jonathan

0 Kudos
leefriendoaklan
Contributor
Contributor
Jump to solution

Hi Jonathanvh,

Looking at this, you may get better performance if you go through the host to get the cluster. Just a thought as a large inventory may take a while to loop through. Try this...

var cluster = ((vm.runtime).host).parent;

OR (if you prefer easier to read code)

var vcVirtualMachineRuntimeInfo = vm.runtime;

var hostSystem = vcVirtualMachineRuntimeInfo.host;

var cluster = hostSystem.parent;

I would suggest that you check the result with something like...

if(cluster.vimType == "ClusterComputeResource"){

    System.log(cluster.name);

}

else

{

    System.log("Standalone_host");

}

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thanks.