VCF Automation Blog

from Stefan Schnell

Get Objects of Inventory Hierarchy


vCenter Hierarchy

vmware vcenter hierarchy

URLs of all Registered vCenter Server Instances as Array of Strings

/**
 * Returns the URLs of all registered vCenter server instances as array
 * of strings.
 *
 * Assembler > Infrastructure > Connections > Cloud Accounts
 * Orchestrator > Administration > Inventory > vSphere vCenter Server
 *
 * Checked with release 8.5.1, 8.12.0, 8.14.1, 8.16.0 and 8.18.0
 */

var vcenters = VcPlugin.allRegisteredInstances;

vcenters.forEach( function(vcenter) {
  System.log(vcenter);
});

All Datacenters as Array of VCDatacenters

/**
 * Returns all datacenters as array of VCDatacenters.
 *
 * Assembler > Infrastructure > Connections > Cloud Accounts
 * Orchestrator > Administration > Inventory > vSphere vCenter Server >
 *   vCenter > Datacenters
 *
 * Checked with release 8.14.1, 8.16.0 and 8.18.0
 */

var datacenters = VcPlugin.getAllDatacenters();

datacenters.forEach( function(datacenter) {
  System.log(datacenter.name + ": " + datacenter);
});

Information of all VMs

/**
 * Returns information of all VMs.
 *
 * Orchestrator > Administration > Inventory > vSphere vCenter Server >
 * vCenter > Datacenters > Datacenter > vm
 *
 * Checked with release 8.12.0, 8.16.0 and 8.18.0
 */
var vms = Server.findAllForType("VC:VirtualMachine", null);

vms.forEach( function(vm) {
  System.log(vm.name);
  // System.log(vm.displayName);
  System.log(vm.hostName);
  System.log(vm.id);
  // System.log(vm.vimId);
  System.log(vm.sdkId);
})

Names of all VMs of a Cluster as Array of Strings

/**
 * Returns the names of all VMs in a cluster as array of strings.
 *
 * Checked with release 8.14.1
 */

function getAllVmsOfCluster(datacenterName, clusterName) {
  /**
   * @function getAllVmsOfCluster
   * @param {string} datacenterName
   * @param {string} clusterName
   * @returns {Array.<string>}
   */
  var vmNames = [];
  var datacenters = VcPlugin.getAllDatacenters([], datacenterName);
  datacenters.forEach( function(datacenter) {
    var children = datacenter.hostFolder.childEntity;
    children.forEach( function(child) {
      if (
        child.vimType === "ClusterComputeResource" &&
        child.name === clusterName
      ) {
        child.host.forEach( function(host) {
          host.vm.forEach( function(vm) {
            vmNames.push(vm.name);
          });
        });
      }
    });
  });
  return vmNames.sort();
}

var vmNames = getAllVmsOfCluster("myDatacenter", "myCluster");

All Datastores as Array of VCDatastores

/**
 * Returns all datastores as array of VCDatastores.
 *
 * Checked with release 8.18.0
 */

const datastores = Server.findAllForType("VC:Datastore", null);

datastores.forEach( function(datastore) {
  System.log(datastore.name);
});