vRealize Orchestrator: Get the datastore containing a VM’s configuration files

1 minute read

I’ve recently had reason to make use of the built-in “Add disk” workflow in vRealize Orchestrator (formerly vCenter Orchestrator) as part of an overall VM provisioning workflow. One of the inputs that “Add disk” requires is a VC:Datastore object where the disk should be placed.

Typically (in my experience), you want new disks to be placed on the same datastore as the VM configuration files. However, there don’t appear to be any out-of-the-box solutions to this particular problem—you have to figure out which datastore is hosting a VM’s config files yourself.

Fortunately, the vSphere API makes this information available in the VirtualMachineFileInfo data object of every VM.

Let’s take a look inside this object with PowerCLI:

PowerCLI> $vm = Get-VM MyVM
PowerCLI> $vm.ExtensionData.Config.Files

VmPathName : [my_datastore] MyVM/MyVM.vmx
SnapshotDirectory : [my_datastore] MyVM/
SuspendDirectory : [my_datastore] MyVM/
LogDirectory : [my_datastore] MyVM/
FtMetadataDirectory :

We can see that the full path to the VMX file is stored in VmPathName. Particularly, the information we need (the name of the datastore) is inside the two square brackets. All that is necessary is to extract the string between those two brackets, and then search inventory for a datastore with that name.

From here, it’s relatively straightforward to create a vRO Action that will accept a VC:VirtualMachine as an input, and return a VC:Datastore.

// INPUT: VC:VirtualMachine in variable 'vm'

var vmxPath = vm.config.files.vmPathName;
var startOfDatastoreName = vmxPath.indexOf('[')+1;
var endOfDatastoreName = vmxPath.indexOf(']');
var vmxDatastore = vmxPath.substring(startOfDatastoreName,endOfDatastoreName);

var xpath = "xpath:name='" + vmxDatastore + "'";
var datastores = VcPlugin.getAllDatastores(null, xpath);

if(datastores.length === 0) {
	throw "Unable to find a datastore named '" + vmxDatastore + "'";
}
else if (datastores.length > 1) {
	throw "Found more than one datastore named '" + vmxDatastore + "'";
}

return datastores[0];