hywards - Fotolia
Use these PowerCLI vMotion cmdlets to automate VM migration
VMware vMotion moves VMs between hypervisors and storage devices to keep those VMs running. You can use PowerCLI to easily automate complex vMotion tasks.
Live VMs are frequently migrated from place to place when IT administrators add or decommission hosts and storage in vCenter. Although a GUI can do the job for one-off tasks, PowerCLI makes complex processes a cinch. With PowerCLI, vMotion and a variety of other complex tasks become easy and automatic via the command-line interface.
VMware vSphere has the magical ability to move a VM not only between hypervisors, but also between storage devices with a feature called vMotion. This process occurs without any VM downtime, which enables you to cluster hypervisors, so you never have to power off a VM.
You can kick off a vMotion task through the vCenter web client, but for more repeatable and complex vMotion tasks, use PowerCLI. PowerCLI can handle just about any possible vMotion scenario. Specifically, you can use the PowerCLI cmdlet Move-VM and additional parameters to perform a variety of vMotion-related tasks.
Move-VM
In PowerCLI, you can use the Move-VM cmdlet to vMotion VMs between both hypervisors and data stores. You can use various parameters with Move-VM, but the simplest example is moving a VM from one host to another. You can move Test-VM to the VM host Test-VMHost:
PS /Users/dan> Move-VM -VM Test-VM -Destination Test-VMHost
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
Test-VM PoweredOn 4 16.000
To move multiple -- up to hundreds -- of VMs, you can use PowerCLI to vMotion VMs to the same host by separating VM names with commas:
PS /Users/dan> Move-VM -VM Test-VM,Test-VM2 -Destination Test-VMHost
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
Test-VM PoweredOn 4 16.000
Test-VM2 PoweredOn 4 16.000
Not only can you move a VM between hosts, but you can add the -Datastore parameter to move a VM to a different data store:
PS /Users/dan> Move-VM -VM Test-VM -Destination Test-VMHost -Datastore Datastore-2
If you only want to move Windows VMs to another host, you can use the Where-Object PowerShell cmdlet to filter out such VMs. Use the "guest" property that the OS on the VM populates:
PS /Users/dan> Move-VM -VM (Get-Cluster 'Cluster2' | Get-VM | Where-Object {$_.Guest -like "*Windows*"}) -Destination VMHost-3
You can also change the format of the disks you move to a different data store with the -DiskStorageFormat parameter. For example, you can move a VM to a thin-disked format:
PS /Users/dan> Move-VM -VM Test-VM -Destination Test-VMHost -Datastore Datastore-2 -DiskStorageFormat thin
If you want to run Move-VM without waiting for the command to complete its process, use the -RunAsync parameter, which returns you to the shell while the cmdlet runs in the background. This helps especially when you move many VMs at once, or when a storage vMotion takes a long time to finish.