Redistribute VMs according to a stored mapping

This is a quick one. Sometimes you need to evacuate VMs from a host, do some maintenance, and redistribute the VMs to the originating host. This can be annoying, especially if you don’t have a vSphere Enterprise Plus license with DRS, DRS groups etc. These few PowerShell lines may help you.

### Discover all VMs in a specific vSphere Cluster
$AllVMs = Get-Cluster PROD | Get-VM | ? { $_.Name -notlike 'vCLS*' }
### Create empty hash table
$hashtable = @{}
### Fill hash table with a mapping VM <> host
foreach ($i in $AllVMs) {
$hashtable.add($i.name, $i.vmhost)
}
### Move VM to the original host
foreach ($i in $hashtable.GetEnumerator()) {
move-vm -VM $($i.Name) -Destination $($i.Value) -RunAsync
}
,