Earlier this week I had a need to move a lot of VM’s from a couple of Hosts to another cluster. And instead of doing it one by one in VMM (Virtual Machine Manager), I wrote a small quick and dirty script that I had not really planned on publishing. Though a customer had a need for that script today, so I figured more people might need it.
Enter the name of the current Host where the VM’s are running.
Enter the name of the destination Hostgroup (seen in VMM). Start script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$VMHost = "FAHOST11" #Name of current Host where VMs are running $HostGroup = "Workload" #Name Hostgroup of Destination Servers $SleepTime = "10" #Seconds to wait between each move. To give the Move time to complete before next start. Else all server might end up on the same Host. ################ $SCHostGroup = Get-SCVMHostGroup -Name $HostGroup $VMs = Get-SCVirtualMachine -VMHost $VMHost foreach ($vm in $VMs) { $BestHost = Get-SCVMHostRating -HighlyAvailable $true -VMHostGroup $SCHostGroup -VM $VM | where VMHost -notlike $VMHost | Sort-Object Rating -Descending | select -first 1 Write-Host $vm $BestHost Move-SCVirtualMachine -VM $vm -VMHost $BestHost.VMHost -HighlyAvailable $true -RunAsynchronously sleep -Seconds $SleepTime } |
The script will calculate the best possible host to move the VM too and then move it there and make it HighAvailable.
I didn’t initially have the sleep line in my script, though I did notice while it was executing that it tried to move too many at the same time (I think the default limit is 2) so some failed. And another issue is that the HostRating may get wrong if its doing a lot of calculations while there are no VMs on the destination host, and then suddenly lots of VMs end up there at the same time. So a sleep should hopefully take care of both those problems at the same time.
Interesting, I would have thought that checking if a node is in maintenance mode was part of the “best node” calculation done by VMM. If I get a chance, I’ll verify and add an extra check to the script.
This is a nice script, but it looks like it doesn’t notice when a node is in maintenance mode. It tried to move some VM’s to a node that was in maintenance mode and all those move jobs failed. Maybe you can add an additional check to exclude nodes in maintenance mode in VMM / paused state in a cluster.