Home » Automation

Tag: Automation

PowerCLI Datastore Selection without Storage DRS (SDRS)

I recently ran into the following problem: the situation occurs when you want to deploy a virtual machines with PowerCLI to a cluster with multiple datastores but your not licensed for Storage DRS.
The default PowerCLI behavior is to select the first datastore detected (alphabetic order). To solve this problem I created the following PowerCLI code:

### Variables
	$CLUSTER = "Production" 					# Cluster name
	$FOLDER = "Deployed VMs"					# Virtual Machine folder

### Select datastores available and sort them on free space (select the one with most space free)
$DS = Get-Cluster -Name $CLUSTER | Get-Datastore | Select Name, FreeSpaceGB | Sort-Object FreeSpaceGB -Descending | Select -first 1

### Create a virtual machine called VM01
New-VM -Name VM01 -ResourcePool $CLUSTER -Datastore $DS.Name -Location $FOLDER -MemoryGB 1 -CD -DiskGB 5

The code is tested with PowerCLI 6.5 R1 against a VMware vCenter 6.0 server.