VMware Cloud Community
tdubb123
Expert
Expert

shutdown Vms and move hosts to another EVC cluster

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

0 Kudos
1 Reply
LucD
Leadership
Leadership

Your script looks ok, it should work.

You can make the VM guest OS shutdown a bit faster by doing them all in one go.

And then waiting in a loop until they are all powered off.

$vms = Get-Datacenter -name $datacenter | Get-Cluster $cluster1| .\Get-VM-sample.png

Shutdown-VMGuest -VM $vms -Confirm:$false

while($vms.PowerState -contains 'PoweredOn'){

    sleep 5

    $vms = Get-Datacenter -name $datacenter | Get-Cluster $cluster1| .\Get-VM-sample.png

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos