VMware Cloud Community
chakoe
Enthusiast
Enthusiast

Shutdown Guest -> move-vm -> power On

Hi All,

i´m looking for a script to:

1) Shutdown a VM

2) Wait for successfull shutdown

3) Move VM to another host

4) Wait for succesfull migration

5) Power On the VM

At the moment, i have the following script:

$vms = Get-VM VM-Name

$desthost = "esxhost.local"

foreach ($singlevm in $vms ) {

Get-VMGuest -VM $singlevm | Shutdown-VMGuest -Confirm:$false

sleep -Seconds "150"

Move-VM -vm $singlevm -Destination $desthost -DiskStorageFormat Thin -Confirm:$false

}

How do i have to build the PowerOn and wait for success-migration? And how do i have to check if the shutdown is complete?

Thx in advance

Chakoe

3 Replies
LucD
Leadership
Leadership

The easiest way is to wait for the events related to these actions.

See Waiting for OS customization to complete.

In your case you would wait for the VmPoweredOffEvent and the VmMigratedEvent events.


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

RvdNieuwendijk
Leadership
Leadership

You can wait for the shutdown to complete with a simple while loop. The Move-VM command will not return untill the migration is finished. So you don't have to check it yourself. The Start-VM cmdlet can be used to start the virtual machine after the migration.

$vms = Get-VM -Name VM-Name

$desthost = Get-VMHost -Name "esxhost.local"

foreach ($singlevm in $vms) {

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

  while ((Get-VM $singlevm).PowerState -ne "PoweredOff") { Start-Sleep -Seconds 5 }

  Move-VM -vm $singlevm -Destination $desthost -DiskStorageFormat Thin -Confirm:$false

  Start-VM -VM $singlevm

}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
system_noida
Enthusiast
Enthusiast

Hi , I know this is very old thread but I need help please. I did use the below code for migrating the VM from one host to another and its working fine. But I am looking to migrate some multiple VMs and want the script to read the VM name and Host name from a CSV file. Please could you let me know what code I have to use in this below script.

 

Thanks

Jitendra Kumar

9910164884

Reply
0 Kudos