Do you need to easily evacuate a clustered vSphere host, and you don’t have DRS (no vSphere Enterprise/Plus licensing)? Then look no further. This little beauty will get you pointed in the right direction, and save you some time.
[powershell]
$viserver = Connect-VIServer vcenter.domain.com -Protocol https
$srchost = Read-Host "FQDN of the ESX server you want to place in maintenance mode:"
$srchost = Get-VMHost -name $srchost
$cluster = Get-Cluster -Name $srchost.Parent
$clusterpartners = Get-VMHost -Location $cluster | where {$_.Name -ne $srchost.Name -and $_.State -eq "Connected"}
$vmguests = Get-VM -Location $cluster
foreach ($vmguest in $vmguests) {
if($vmguest.Host -like $srchost.Name){
$dsthost = $clusterpartners | Get-Random
Move-VM -VM $vmguest -Destination $dsthost -Confirm:$false -RunAsync:$true
}
}
Set-VMHost -VMHost $srchost -State "Maintenance"
[/powershell]
Daniel says
Nice Script 🙂 Do you think, there is an option getting the exact same VM’s Back on the host ? The random parameter spreads the VM’s accross the cluster, but how do i get them back ? Any ideas ?
Greetings,
Daniel.
Damian Karlson says
Hi Daniel — thanks for commenting. What you want to do could be accomplished by populating an array with the VM name and source host, and then “replaying” that array to get everything moved back to the previous host. In my case, I wasn’t too concerned with guest distribution across the cluster, but in clusters where you’ve manually load-balanced, I can understand the benefit. Hope this helps!
D