Skip to content

Instantly share code, notes, and snippets.

@PatrickTerlisten
Created January 15, 2023 11:04
Show Gist options
  • Save PatrickTerlisten/d467eb25d2bf81a297989a562d583b88 to your computer and use it in GitHub Desktop.
Save PatrickTerlisten/d467eb25d2bf81a297989a562d583b88 to your computer and use it in GitHub Desktop.
Save a VM <> host mapping and redistribute VMs according to the stored mapping
### 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment