I am trying to find a way in powercli to change the Storage DRS Automation Level to the Default (Fully Automated) on all my VMs.
$si = Get-View ServiceInstance
$srmMgr = Get-View -Id $si.Content.StorageResourceManager
foreach($dsc in Get-DatastoreCluster){
$spec = New-Object VMware.Vim.StorageDrsConfigSpec
foreach($vm in ($dsc.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig | where{$_.Behavior -eq [VMware.Vim.StorageDrsPodConfigInfoBehavior]::manual})){
$vmSpec = New-Object VMware.Vim.StorageDrsVmConfigSpec
$vmSpec.Operation = [VMware.Vim.ArrayUpdateOperation]::edit
$vmSpec.Info = $vm
$vmSpec.Info.Behavior = [VMware.Vim.StorageDrsPodConfigInfoBehavior]::FullyAutomated
$spec.VmConfigSpec += $vmSpec
}
$srmMgr.ConfigureStorageDrsForPod($dsc.ExtensionData.MoRef,$spec,$true)
}
And what is the problem with this code?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
There is no error, but the VMs are not changing status to the Default
The enum is not correct, that should be
$vmSpec.Info.Behavior = [VMware.Vim.StorageDrsPodConfigInfoBehavior]::automated
This only changes the VMs for which there is already a VM Override and provided that Override is set to manual.
That code will not create new VM Overrides entries.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ah ok, I need to learn how to remove the overrides at all.
You could do
$srmMgr = Get-View StorageResourceManager
foreach ($dsc in Get-DatastoreCluster) {
$spec = New-Object VMware.Vim.StorageDrsConfigSpec
$dsc.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig |
ForEach-Object -Process {
$vmSpec = New-Object VMware.Vim.StorageDrsVmConfigSpec
$vmSpec.Operation = [VMware.Vim.ArrayUpdateOperation]::Remove
$vmSpec.RemoveKey = $_.Vm
$spec.VmConfigSpec += $vmSpec
}
$srmMgr.ConfigureStorageDrsForPod($dsc.ExtensionData.MoRef, $spec, $true)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference