LuckyT
Contributor
Contributor

Script to get Datacenter Tree

Jump to solution

Hi All,

I want to return full tree of the Datacenters through scriptable task.

example:

Datacenter1:

     Cluster1:

          Host1:

          Host2:

          VM1:

          VM2

When i try to enumerate Datacenter to get Cluster it fails. Can anyone can help on this?

MyScript:

var myDatacenters =  VcPlugin.getAllDatacenters();

for each (i=0;i<myDatacenters.length;i++)

{

    for each ( myCluster in myDatacenters[i].cluster ) 

    { 

        System.debug(cluster.name);

        for each ( myHostSystem in myCluster.host ) 

        { 

            System.debug(myHostSystem.name);

            for each ( myVMs in myHostSystem.vm ) 

            {

                if(myVMs.isTemplate==true)

                {

                    System.debug(myVMs.name);

                }

       

            } 

        }

    for each ( myDatastore in myCluster.datastore ) 

    { 

        System.debug(myDatastore.name);

         //mytotalDatastoreCapacity += myVcDatastore.summary.capacity; 

         //mytotalDatastoreFreeSpace += myVcDatastore.summary.freeSpace;

     

    }

    }

}

0 Kudos
1 Solution

Accepted Solutions
ChristianWehner
VMware Employee
VMware Employee

Hi LuckyT,

there is no attribute VcDatacenter.cluster, you could use VcPlugin.getAllClusterComputeResources() instead.

But I found some code on my environment which maybe also faces your needs:

for each ( var myDatacenter in VcPlugin.allDatacenters ) {

  forEachChildEntity ( myDatacenter );

}

function forEachChildEntity( myChildEntity ) {

  switch ( true ) {

  case myChildEntity instanceof VcDatacenter:

  System.log( "Datacenter:" + myChildEntity.name );

  for each ( var myChildEntitysChildEntity in myChildEntity.hostFolder.childEntity ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  for each ( var myChildEntitysChildEntity in myChildEntity.datastoreFolder.childEntity ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  for each ( var myChildEntitysChildEntity in myChildEntity.networkFolder.childEntity ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  for each ( var myChildEntitysChildEntity in myChildEntity.vmFolder.childEntity ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  break;

  case myChildEntity instanceof VcFolder:

  System.log( "Folder:" + myChildEntity.name );

  for each ( var myChildEntitysChildEntity in myChildEntity.childEntity ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  break;

  case myChildEntity instanceof VcClusterComputeResource:

  System.log( "Cluster:" + myChildEntity.name );

  for each ( var myChildEntitysChildEntity in myChildEntity.resourcePool.resourcePool ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  break;

  case myChildEntity instanceof VcResourcePool:

  System.log( "ResourcePool:" + myChildEntity.name );

  for each ( var myChildEntitysChildEntity in myChildEntity.resourcePool ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  break;

  case myChildEntity instanceof VcVmwareDistributedVirtualSwitch:

  System.log( "DV Switch:" + myChildEntity.name );

  for each ( var myChildEntitysChildEntity in myChildEntity.portgroup ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  break;

  case myChildEntity instanceof VcDistributedVirtualPortgroup:

  System.log( "Portgroup:" + myChildEntity.name );

  break;

  case myChildEntity instanceof VcStoragePod:

  System.log( "StoragePod:" + myChildEntity.name );

  break;

  case myChildEntity instanceof VcVirtualMachine:

  System.log( "VirtualMachine:" + myChildEntity.name );

  break;

  default:

  System.log( "Unknown:" + myChildEntity.name );

  break;

  }

}

Regards,

Chris

View solution in original post

0 Kudos
2 Replies
ChristianWehner
VMware Employee
VMware Employee

Hi LuckyT,

there is no attribute VcDatacenter.cluster, you could use VcPlugin.getAllClusterComputeResources() instead.

But I found some code on my environment which maybe also faces your needs:

for each ( var myDatacenter in VcPlugin.allDatacenters ) {

  forEachChildEntity ( myDatacenter );

}

function forEachChildEntity( myChildEntity ) {

  switch ( true ) {

  case myChildEntity instanceof VcDatacenter:

  System.log( "Datacenter:" + myChildEntity.name );

  for each ( var myChildEntitysChildEntity in myChildEntity.hostFolder.childEntity ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  for each ( var myChildEntitysChildEntity in myChildEntity.datastoreFolder.childEntity ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  for each ( var myChildEntitysChildEntity in myChildEntity.networkFolder.childEntity ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  for each ( var myChildEntitysChildEntity in myChildEntity.vmFolder.childEntity ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  break;

  case myChildEntity instanceof VcFolder:

  System.log( "Folder:" + myChildEntity.name );

  for each ( var myChildEntitysChildEntity in myChildEntity.childEntity ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  break;

  case myChildEntity instanceof VcClusterComputeResource:

  System.log( "Cluster:" + myChildEntity.name );

  for each ( var myChildEntitysChildEntity in myChildEntity.resourcePool.resourcePool ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  break;

  case myChildEntity instanceof VcResourcePool:

  System.log( "ResourcePool:" + myChildEntity.name );

  for each ( var myChildEntitysChildEntity in myChildEntity.resourcePool ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  break;

  case myChildEntity instanceof VcVmwareDistributedVirtualSwitch:

  System.log( "DV Switch:" + myChildEntity.name );

  for each ( var myChildEntitysChildEntity in myChildEntity.portgroup ) {

  forEachChildEntity( myChildEntitysChildEntity );

  }

  break;

  case myChildEntity instanceof VcDistributedVirtualPortgroup:

  System.log( "Portgroup:" + myChildEntity.name );

  break;

  case myChildEntity instanceof VcStoragePod:

  System.log( "StoragePod:" + myChildEntity.name );

  break;

  case myChildEntity instanceof VcVirtualMachine:

  System.log( "VirtualMachine:" + myChildEntity.name );

  break;

  default:

  System.log( "Unknown:" + myChildEntity.name );

  break;

  }

}

Regards,

Chris

0 Kudos
LuckyT
Contributor
Contributor

Thanks, this is very useful.

0 Kudos