Automation Blog

from Stefan Schnell

Get Objects of Inventory Hierarchy


vCenter Hierarchy

Registered vCenter Server Instances

/**
 * 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 Aria Automation 8.5.1, 8.12.0, 8.14.1,
 * 8.16.0, 8.18.0 and VCF Automation 9.0.0
 */

// Configuration level, delivers all vCenters
// registered in the Orchestrator
var vcenters = VcPlugin.allRegisteredInstances;

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

// Live connection level, delivers all vCenters
// that have successfully established a connection
var vCenters = VcPlugin.allSdkConnections;

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

Datacenters

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

var datacenters = VcPlugin.getAllDatacenters();

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

Managed Entities of the Folder

/**
 * Returns managed Entities
 *
 * Checked with release Aria Automation 8.18.0 and
 * VCF Automation 9.0.0
 */

var datacenters = VcPlugin.getAllDatacenters([], "");
datacenters.forEach( function(datacenter) {
  System.log(datacenter.name);
  var children = datacenter.hostFolder.childEntity; // Cluster
//  var children = datacenter.vmFolder.childEntity;
//  var children = datacenter.networkFolder.childEntity;
//  var children = datacenter.datastoreFolder.childEntity;
  children.forEach( function(child) {
    System.log(child.vimType + " - " + child.name);
  });
});

Information of all VMs

/**
 * Returns information of all VMs.
 *
 * Orchestrator > Administration > Inventory > vSphere vCenter Server >
 * vCenter > Datacenters > Datacenter > vm
 *
 * Checked with release Aria Automation 8.12.0, 8.16.0, 8.18.0 and
 * VCF Automation 9.0.0
 */

// Important hint
// Requests for all VMs without a filter (query = null) should be
// avoided with both methods, an XPath filter should be used.

// Search in the internal database or local cache of the Orchestrator
// for all VM objects
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);
});

// Search in all connected vCenter servers for all VM objects
var vms = VcPlugin.getAllVirtualMachines(null, null);

vms.forEach( function(vm) {
  System.log(vm.name);
  // ...
});

// Examples how to us an XPath query
var xPath = "xpath:name='vm-0001'";
// var xPath = "xpath:starts-with(name, 'v')"
var vms = VcPlugin.getAllVirtualMachines(null, xPath);

VMs of a Cluster

/**
 * Returns the names of all VMs in a cluster as array of strings.
 *
 * Checked with release Aria Automation 8.14.1, 8.18.0 and
 * VCF Automation 9.0.0
 */

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");

// Example for the HOL 8.18
// var vmNames = getAllVmsOfCluster(
//   "mgmt-datacenter-01",
//   "mgmt-cluster-01"
// );

// Example for the HOL 9.0
// var vmNames = getAllVmsOfCluster(
//   "wld-01a-DC",
//   "cluster-wld01-01a"
// );

Datastores

/**
 * Returns all datastores as array of VCDatastores.
 *
 * Checked with release Aria Automation 8.18.0 and
 * VCF Automation 9.0.0
 */

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

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