Setting VM SDRS automation levels

I learnt recently that when you Storage vMotion a VM within a datastore cluster and you check the box to select which datastore you explicitly want the VM to be migrated to, it sets the SDRS automation level for the VM to disabled.

Finding out which VMs have an automation level of disabled can be achieved with the following line:

Get-DatastoreCluster | % { $_.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig } | ? { $_.Enabled -eq $False } | % { Get-View $_.VM | select Name }

Since I like to have VMs set to the cluster default we need a bit of PowerCLI to change the VMs back. The following code will look at all the datastore clusters on the vCenter, and all the VMs with a disabled automation level therein.

Credit goes to LucD whose posts in the VMware Communities forum formed the base of this script. The threads are here and here.

This script is designed to be used with PowerCLI 6, as per the “Import-Module” statements at the top. For previous versions of PowerCLI the lines should be replaced with adding the appropriate snapin.

param([Parameter(Mandatory=$true)][String] $VIServer)

Import-Module VMware.VimAutomation.Core;
Import-Module VMware.VimAutomation.Storage;

Connect-VIServer $VIServer

$storMgr = Get-View StorageResourceManager;

Get-DatastoreCluster |
	% { $vms = $_.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig;
		$spec = New-Object VMware.Vim.StorageDrsConfigSpec;
		$vms | ? { $_.Enabled -eq $False } | % {
			$entry = New-Object VMware.Vim.StorageDrsVmConfigSpec;
			$entry.Operation = "edit";
			$entry.Info = $_;
			$entry.Info.Enabled = $true;
			$spec.vmConfigSpec += $entry;
		$storMgr.ConfigureStorageDrsForPod($_.ExtensionData.MoRef, $spec, $true);
Disconnect-VIServer -Confirm:$False

If needed, this script can easily be expanded to check for Tags or Custom Attributes assigned to the VM, and only to change the automation level given the presence or absence of a tag or the value of a custom attribute.