VMware Cloud Community
tdubb123
Expert
Expert

shutdown and migrate VMs in groups

I have a csv file like this

VM
SRC_ClusterDST_CLUSTER
Group
vm1clus1clus21
vm2clus1clus21
vm3clus1clus22
vm4clus1clus22
vm5clus1clus23
vm6clus1clus23

I want to shutdown Group 1 guests, move them from clus1 to clus2. power them on the new cluster. Wait until they are all powered on then move on to the next group.

mport-csv "c:\scripts\vms.csv" -useculture | %{

$vm = $_.VM

$src_cluster = $_.SRC_Cluster

$dst_Cluster = $_.DST_Cluster

$group = $_.group

write-host "shutting down prod-vm $vm"

$vm | stop-vmguest -confirm:$false

    while($vm.PowerState -ne 'PoweredOff'){

        $vm = Get-VM -Name $vm.Name

        sleep 5

move-vm $vm -location $dst_cluster

start-vm $vm -confirm:$false

}

}

how do I pause from group to group?

0 Kudos
1 Reply
LucD
Leadership
Leadership

You could do something like this.
It organises the CSV into groups, and then for all the VM in each group, it will stop them, nove them and power them on.
After the power off and power on, the script waits n a loop till all VMs have completed the action.

Import-csv "c:\scripts\vms.csv" -UseCulture |

Group-Object -Property Group | %{

    Get-VM -Name $_.Group.VM | Stop-VMGuest -Confirm:$false

   

    while((Get-VM -Name $_.Group.VM).PowerState -contains 'poweredon'){

        sleep 5

    }

    Move-VM -VM $_.Group.VM -Destination (Get-Clsuter -Name $_.Group[0].DST_CLUSTER) -Confirm:$false

    Start-VM -VM $_.Group.VM -Confirm:$false

    while((Get-VM -Name $_.Group.VM).PowerState -contains 'poweredoff'){

        sleep 5

    }

}


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

0 Kudos