I got hosts on a EVC disabled cluster with running VMs that I need to move all the host to another cluster. To do that I need to shutdown all the VMs.
Will this work? I have not tested and unable to test
Connect-VIServer vcenter
$Datacenter = "DC”
$cluster1 = “cluster01”
$cluster2 = “Cluster02”
$sleep = 1
$vms = Get-datacenter -name $datacenter | get-cluster $cluster1| get-vm
$vmhosts = get-cluster $cluster1 | get-vmhost
#Shutdown all VMs
Foreach ($vm in $vms) {
If ($vm.powerstate -eq “PoweredOn”) {
Write-host “Shutting down” $vm
Shutdown-vmguest -vm $vm
#wait for all shutdown to complete
Do {
Start-sleep -s 5
#check power status
$vm = get-vm -name $vm
$Status = $vm.powerstate
} until ($status =eq “PoweredOff”)
# put hosts in maintenance mode and disconnect and move to new cluster
Foreach ($vmhost in $vmhosts) {
Set-vmhost -name $vmhost -state Maintenance -confirm:$false
Set-vmhost -name $vmhost -state disconnected -confirm:$false
Move-vmhost -location (get-cluster -name $cluster2) -confirm:$false
Set-vmhost -name $vmhost -state -connected -confirm:$false
Sleep $sleep
}
# startup all VMs in new cluster
Foreach ($vm in $vms) {
If ($vm.powerstate -eq “PoweredOff”) {
Write-host “Starting up” $vm
Start-vm -name $vm -confirm false -runasync
disconnect-viserver vcenter