VMware Cloud Community
Skagnola
Enthusiast
Enthusiast
Jump to solution

Move-VM between compute resources / datastores

In the near future, my environment may need to move 100+ VMs from one compute resource / datastores to a different compute / datastore. I am able to successfully test doing a single move like so:

 

Move-VM -VM testvm -Destination [destination host in the cluster} -Datastore [destination ds in the cluster]

 

However, if I want to allow vMotion to automatically determine what destination host and datastore to move the VMs to, what are the arguments and how to move these VMs? Also, if I want to set a move limit such that only 2 or three are moved at a time?

In the GUI, when choosing to migrate both compute / datastore you are prompted for the top-level cluster resource and does not require the specific host. The datastore it requires you to choose the store. 

I am fairly new to PowerCLI /PowerShell so any help is appreciated. Thanks!

0 Kudos
21 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this.

Note that I take a new randon ESXi node for each VM, otherwise all your VMs owuld be moved to the same ESXi node in the cluster.

Also note that since you are moving VM, the freespace on the datastores will change, hence we calculate that one as well again for each VM.

$tgtClusterName = 'K7C'

$cluster = Get-Cluster -Name $tgtClusterName

$vmList = Get-Content C:\vms.txt

foreach ($vm in $vmList) {

    $esx = Get-VMHost -Location $cluster | Get-Random

    $ds = Get-Datastore -Name k7c*-cml*-brhev* | Sort-Object -Property FreeSpaceGB -Descending | Select -First 1

    VMware.VimAutomation.Core\Move-VM -VM $vm -Destination $esx -Datastore $ds

}


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

0 Kudos
Skagnola
Enthusiast
Enthusiast
Jump to solution

That worked! I had a file with 4 VMs and it went through them one at a time, completing the move.

Thank you so much! I would dance at your wedding, if I could! Smiley Happy

0 Kudos