VMware Cloud Community
markymark007
Contributor
Contributor

Migrate VM to different cluster

I am having trouble getting my head around how to approch this script.

The big picture is that I need a script to migrate all VM within a resource pool to a diffrent cluster.

Things I have to concider are.

  • The resource pool name in both source and destination clusters is the same.
  • We have a high turn around of hosts, so not including a direct referance to a single host would be prefered.

I have been trying to achive this by use the MOVE-VM command but I am hitting a wall as follows: 

Move-VM        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.

My question is: What method can be used to get the VMs into the destination RP on the destination cluster? 

Any help wth this would be great...thanks!

0 Kudos
3 Replies
LucD
Leadership
Leadership

Afaik, the current implementation of the Move-VM cmdlet doesn't allow you to use all possibilities that the underlying MigrateVM_Task method offers.

So you could try calling the SDK method directly.

Something like this

$rp = Get-ResourcePool Test -Location (Get-Cluster -Name CLuster1)
$vm = Get-VM -Name TestVM -Location $rp 
$tgtRp
= Get-ResourcePool Test -Location (Get-Cluster -Name Cluster2) $vm.ExtensionData.MigrateVM($tgtRp.Extensiondata.MoRef,$null,"defaultPriority","poweredOff")

This should move the VM, called TestVM, from the resource pool, called Test, in cluster Cluster1 to a resource pool, also called Test, in cluster Cluster2.

The conditions for this to work:

  • the target cluster needs DRS to be active
  • the datastore on which the VM is stored, must be visible and defined on both clusters


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

0 Kudos
markymark007
Contributor
Contributor

LucD,

This is why your the vExpert!  Great stuff.

I have a few more questions for you if that is ok.

I haven't had much exposure to the SDK method being used here.   Say I wanted to extend the script to include ALL the VMs within the source RP with ANY power state.  How would that look?   The end goal would be to present a pop up asking the admin for the RP name when the script runs.  Then ALL the VMs will be vMotioned from the source cluster to the target cluster.

Looking forward to your reponse.  Smiley Happy

0 Kudos
LucD
Leadership
Leadership

The MigrateVM_Task method needs to be called from a specific VM.

But you could write the code around it in a script, something like this

$srcClusterName = "Cluster1" 
$tgtClusterName
= "Cluster2"
$srcCluster
= Get-Cluster -Name $srcClusterName
$tgtCluster = Get-Cluster -Name $tgtClusterName
$rpName
= Read-Host -Prompt "Enter the name of the resource pool. Q to stop"
while($rpName -ne "Q"){     $srcRP = Get-ResourcePool -Name $rpName -Location $srcCluster -ErrorAction SilentlyContinue
    if($srcRP){         $tgtRP = Get-ResourcePool -Name $rpName -Location $tgtCluster
        Get-VM -Location $srcRP | %{             $_.ExtensionData.MigrateVM($tgtRp.Extensiondata.MoRef,$null,"defaultPriority",$null)         }     }     else{         Write-Error -Message "Resource pool $rpName can't be found."
    }     $rpName = Read-Host -Prompt "Enter the name of the resource pool. Q to stop"
}

Make sure to test this before running it in your production environment !


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

0 Kudos