I was trying to move a vm from one datacenter to the other. The destination has 2 host clusters, each with multiple hosts. All hosts connect to the same datastore cluster.
Tried this but got an error: must be on the same datacenter:
$vm | Move-VM -Destination $vmHost -Datastore $ds
But if I tried -Destination as $datacenter object, PowerCLI didn't throw an error:
$vm | Move-VM -Destination $datacenter -Datastore $ds
However, the when I looked at the vSphere Client, it showed an error:
The operation is not supported on the object.
What's the best way to move a vm to a different datacenter?
The alternative is to use the RelocateVM_Task method.
Something like this
$vmName = "TestVM"
$tgtDatastore = "datastore"
$tgtCluster = "cluster"
$tgtPool = "resourcepool"
$vm = Get-VM -Name $vmName
$ds = Get-Datastore -Name $tgtDatastore
$esx = Get-Cluster -Name $tgtCluster | Get-VMHost | Get-Random
$rp = Get-ResourcePool -Name $tgtPool
$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
$spec.Datastore = $ds.ExtensionData.MoRef
$spec.Host = $esx.ExtensionData.MoRef
$spec.Pool = $rp.ExtensionData.MoRef
$vm.ExtensionData.RelocateVM($spec,"defaultPriority")
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That is afaik a limitation, you can only migrate between clusters in the same datacenter.
I suspect the VM was powered on ?
See here for the limitations of vMotion
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
VMs were powered off. When manually moving them, I could specify which host and datastore to move to, then the VMs would be placed in the root of target datacenter folder. Then I could move them to the subfolder I specify. So it's a 2-step process if I do this through vSphere client.
The alternative is to use the RelocateVM_Task method.
Something like this
$vmName = "TestVM"
$tgtDatastore = "datastore"
$tgtCluster = "cluster"
$tgtPool = "resourcepool"
$vm = Get-VM -Name $vmName
$ds = Get-Datastore -Name $tgtDatastore
$esx = Get-Cluster -Name $tgtCluster | Get-VMHost | Get-Random
$rp = Get-ResourcePool -Name $tgtPool
$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
$spec.Datastore = $ds.ExtensionData.MoRef
$spec.Host = $esx.ExtensionData.MoRef
$spec.Pool = $rp.ExtensionData.MoRef
$vm.ExtensionData.RelocateVM($spec,"defaultPriority")
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you Luc. Calling RelocateVM_Task works!
Jason
How would I run the RelocateVM_Task with something similar to -RunAsync??
Thanks!
Most of the API methods have 2 invocations, one with a _Task suffix and one without that suffix.
The _Task entry point is the RunAsync one.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
When I try this code in 6.0 I just get multiple errors.
$vmName = "MYVM"
$tgtDatastore = "TestSAN"
$tgtCluster = "GD SVRM"
$tgtPool = "Resources" - I notice i cannot define a resource pool for this DC.
$vm = Get-VM -Name $vmName
$ds = Get-Datastore -Name $tgtDatastore
$esx = Get-Cluster -Name $tgtCluster | Get-VMHost | Get-Random
$rp = Get-ResourcePool -Name $tgtPool
$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
$spec.Datastore = $ds.ExtensionData.MoRef
$spec.Host = $esx.ExtensionData.MoRef
$spec.Pool = $rp.ExtensionData.MoRef
$vm.ExtensionData.RelocateVM($spec,"defaultPriority")
Exception setting "Datastore": "Cannot convert the "System.Object[]" value of
type "System.Object[]" to type "VMware.Vim.ManagedObjectReference"."
At C:\movevm.ps1:12 char:1
+ $spec.Datastore = $ds.ExtensionData.MoRef
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationExceptio
n
+ FullyQualifiedErrorId : ExceptionWhenSetting
Exception setting "Pool": "Cannot convert the "System.Object[]" value of type
"System.Object[]" to type "VMware.Vim.ManagedObjectReference"."
At C:\movevm.ps1:14 char:1
+ $spec.Pool = $rp.ExtensionData.MoRef
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationExceptio
n
+ FullyQualifiedErrorId : ExceptionWhenSetting
Exception calling "RelocateVM" with "2" argument(s): "A specified parameter
was not correct: spec.location.datastore"
At C:\movevm.ps1:15 char:1
+ $vm.ExtensionData.RelocateVM($spec,"defaultPriority")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
Any help would be appreciated.
It looks as if you have multiple ResourcePool objects returned, hence the "System.Object[]" in the error message.
If you have multiple vSphere server connections open (check $global:defaultviservers), add a Server parameter on the Get-ResourcePool cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference