SCVMM 2012 Evacuate VMHost and maintenance script
A customer found the bug that exists in the System Center Virtual Machine Managers function for setting a host in maintenance mode. The bug is that when you set a host in maintenance mode it will live migrate all VM´s to the next node in the cluster. This is kind of impractical when you for example want to patch the Hosts and you end up with the last host that is filled with in worst case all VM´s on it and it takes longer time to live migrate them. The VMM bug should be fixed in the SC SP1 that is coming soon This of course depending on if you have enabled the Dynamic Optimization, that function helps you with the distribution but that will take some time before it runs.
He made a script for evacuating the host and distributing the VMs to the other nodes in the cluster. I have added a bit of logic that also set the host group to not allow dynamic optimization during the scripted evacuation.
The load balancing in my script is quite easy because right now the only thing I look at is the host memory and migrate the VM to the Host that has most memory left at the moment.
<# .Synopsis A function to evacuate a host in vmm and set it in maintence mode .DESCRIPTION This function migrates all your VM´s to other nodes in the cluster based on available Host memory .EXAMPLE Evacuate-SCVMHost -VMHost hyp02 -HostGroup DC01 .Notes Niklas Akerlund/Lumagate 2012-11-25 #> function Evacuate-SCVMHost { [CmdletBinding()] [OutputType([int])] Param ( # What VMHost you want to set to maintmode [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] $VMHost, # In what hostgroup the host resides $HostGroup ) # Get The cluster and disable the dynamic optimization during evac $HostGroup = Get-SCVMHostGroup $HostGroup $HostGroup | Get-SCDynamicOptimizationConfiguration | Set-SCDynamicOptimizationConfiguration -ManualMode $VMHost = Get-VMHost $VMHost # Evacuate the host $VMs = $VMHost | Get-VM foreach ($VM in $VMs){ # Find the most apropriate Host in cluster for each VM $VMHostTarget = Get-SCVMHostCluster -VMHostGroup $HostGroup | Get-SCVMHost | where {$_.ComputerName -ne $VMHost.ComputerName} | Sort-Object AvailableMemory -Descending | Select-Object -First 1 Move-SCVirtualMachine -VM $VM -VMHost $VMHostTarget } # Set the host in maintmode Disable-VMHost -VMHost $VMHost -MoveWithinCluster # Enable dynamic optimization $HostGroup | Get-SCDynamicOptimizationConfiguration | Set-SCDynamicOptimizationConfiguration -AutomaticMode