Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trisharia/a73f7b8915d490486c7fa79624aaa776 to your computer and use it in GitHub Desktop.
Save trisharia/a73f7b8915d490486c7fa79624aaa776 to your computer and use it in GitHub Desktop.
Get all compatible Datastores for a vCenter Host and VM
// VMware vRealize Orchestrator action sample
//
// Get all compatible datastores for a given host, resource pool, and virtual machine.
// The datstore must have enough free storage space necessary for the VM, and must be
// accessible and not in an overall status of "Red".
//
// For vRO 7.0+/vCenter 6.0+
//
// Action Inputs:
// host - VC:HostSystem - vCenter host
// vm - VC:VirtualMachine - vCenter VM
// pool - VC:Resource Pool - vCenter resource pool
//
// Return type: Array/VC:Datastore - datastores compatible with the given host and VM
var compatibleDatastores = [];
// use OOTB function to find all datastores for the given host/pool/VM
var allDatastores = System.getModule("com.vmware.library.vc.datastore").getDatastoreForHostAndResourcePool(host,pool,vm);
var vmStorageNeeded = vm.summary.storage.committed;
System.debug("vm.summary.storage.committed (MB): " + (vmStorageNeeded / 1024 / 1024));
for each (var datastore in allDatastores) {
datastore.refreshDatastore();
System.debug("Checking compatibility of datastore: " + datastore.name);
System.debug(" datastore.overallStatus: " + datastore.overallStatus);
System.debug(" datastore.summary.accessible: " + datastore.summary.accessible);
System.debug(" datastore.info.freeSpace (MB): " + (datastore.info.freeSpace / 1024 / 1024));
if (datastore.summary.accessible
&& datastore.overallStatus != VcManagedEntityStatus.red
&& datastore.info.freeSpace >= vmStorageNeeded) {
compatibleDatastores.push(datastore);
}
}
return compatibleDatastores;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment