VMware Cloud Community
AndersF
Contributor
Contributor

Question regarding Move-Vm

Hi,

Background,
I need to move alot of VM's from cluster A to cluster B. Cluster A and B have shared storage. Easy enough i thought and tried to use Move-VM. 

Problem:
The VM's are located in Resourcepools. It looks like i can't do something like,

Move-VM -VM $VMs -Destination ClusterB -Destination ResourcePoolB

Parameters on Move-VM states "Specify a folder, host, cluster, or a resource pool where you want to move the virtual machines."
From that i understand that what im trying to do is not doable, i have to pick on of them.

Anyone have a suggestion?

0 Kudos
16 Replies
LucD
Leadership
Leadership

You can't have two Destination parameters.

You pick the target resourcepool, which implicitly contains the selected cluster.

Something like this

$rp = Get-Cluster -Name ClusterB | Get-ResourcePool -NameResourcePoolB

Move-VM -VM $vms -Destination $rp


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

0 Kudos
AndersF
Contributor
Contributor

Ah of course! I knew i was missing something obvious.
Thanks alot for the fast reply!

0 Kudos
AndersF
Contributor
Contributor

Seems like that din't work either

error:

Destination is a Resource Pool owned by a Cluster, but the VM you are trying to move is not in that Cluster. Please select for destination 
a Host in that Cluster or a Resource Pool owned by a standalone Host.

Destination is a Resource Pool owned by a Cluster, but the VM you are trying to move is not in that Cluster. Please select for destination 
a Host in that Cluster or a Resource Pool owned by a stanalone Host.
0 Kudos
LucD
Leadership
Leadership

Which vSphere and PowerCLI versions are you using?


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

0 Kudos
AndersF
Contributor
Contributor

vSphere 6.5 u2 and PowerCLI 6.5.1
0 Kudos
LucD
Leadership
Leadership

Is that PowerCLI 6.5R1 (the one from the MSI file) or 6.5.1 (the one from the PSGallery)?


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

0 Kudos
LucD
Leadership
Leadership

As an alternative, does this work?

$cluster = Get-Cluster -Name ClusterB

$rp = Get-ResourcePool -Name ResourcePoolB -Location $cluster

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


Move-VM -VM $vms -Destination $esx |

Move-VM -Destination $rp


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

0 Kudos
AndersF
Contributor
Contributor

Thanks alot for all input!

Move-VM -VM $vms -Destination $esx |
Move-VM -Destination $rp

Doing like you did Move-VM get an parsing error so i just removed | and did like this

Move-VM -VM $VMs[0..1] -Destination $ESX 
Move-VM -VM $VMs[0..1] -Destination $rp

This works fine, to the root first then into the correct resourcepool at the destination cluster. Now i just have to figure out how to pace this since i dont want to que up 300 vMotions to the $esx since that will be the same host during the whole move.

Move-VM -VM $VMs[0..1] -Destination $ESX 
Move-VM -VM $VMs[0..1] -Destination $rp
0 Kudos
LucD
Leadership
Leadership

You can use the RunAsync switch on the Move-VM cmdlet, but then you will have to keep track of the background tasks.

Especially since there are 2 Move-VM cmdlet you have to do, and they have to be in the correct sequence, and the 2nd one can't start before the 1st one has completed.

An alternative is to use PowerShell background job with the Start-Job cmdlet.


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

0 Kudos
AndersF
Contributor
Contributor

I looked a bit at the RunAsync, looks like it can get a bit messy. I will take a look at Start-Job cmdlet.

0 Kudos
AndersF
Contributor
Contributor

Can i  just use what i have and somehow refresh $esx = Get-VMHost -Location $DSTCluster | Get-Random after every move?

Move-VM to cluster
Move-VM to resourcepool
Get-VMHost -Location $DSTCluster | Get-Random
Maybe a Start-Sleep

This way a i can drain the cluster slow and steady and not end up with all VMs on one host. I have been looking around and havent found a good way to do as i describe above. As you all can tell by now im new to this so bear with me 🙂 Why couldent jsut the Move-VM command let ju move things to a new cluster/resourcepool instead of just one of them 🙂

0 Kudos
LucD
Leadership
Leadership

Sure you can do that, and that is indeed a good approach to spread the load.

I'm not sure why you can't go for a cluster/resourcepool as the target.
That looks to be a PowerCLI thing, since the underlying API method seems to allow that.

Also note that there is a builtin max for the number of parallel vMotions you can perform.
Kind of a builtin safety valve


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

0 Kudos
AndersF
Contributor
Contributor

Anyone have an example how to do that, i can't figure out how to loop the whole thing around for each VM.
0 Kudos
LucD
Leadership
Leadership

Assuming that you have all the VMs you want to move in the $vms variable, you could do something like this.

Each run through the ForEach loop will pick a new random ESXi node in the cluster.

$cluster = Get-Cluster -Name ClusterB

$rp = Get-ResourcePool -Name ResourcePoolB -Location $cluster


foreach ($vm in $vms) {

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

  Move-VM -VM $vm -Destination $esx |

   Move-VM -Destination $rp

}


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

0 Kudos
AndersF
Contributor
Contributor

hmm that actually triggers the Move-VM of all $VMs for every object in the array. So if you have 10 Servers in $VMs it will run 9 times and don't find anything to move. I will try figure something out tomorrow. Thanks again for all help!
0 Kudos
LucD
Leadership
Leadership

Oops, my bad, I used $vms where I should have used $vm.
I updated the code.


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

0 Kudos