How to Migrate VMs to New vCenter using PowerCLI

I faced this same question recently in my work environment. I was taking over the management of some infrastructure for a remote site, along with adding new hardware. The existing hardware was in a standalone vCenter, and the new hardware was connected to our enterprise vCenter. Lucky for me, the standalone vCenter was version 6.7 and has the ability to run the PowerCLI cross vCenter migrations. There are a lot of moving parts to this, such as have open ports between the vCenter servers and a few other gotchas. So if this is something you need, read about it at the follow the link (https://kb.vmware.com/s/article/2106952) as I’m only going over the PowerCLI stuff.

With that part out of the way, the PowerCLI portion of this is incredible awesome. It only took 8 lines to get the VMs to migrate from vCenter to vCenter, one at a time.

$NewvCenter = Connect-VIServer [New_vCenter_Name] -Credential $(Get-Credential)
$NewDatastore = Get-Datastore [New_Datastore_Name] -Server $NewvCenter
$NewVMHost = Get-VMhost [New_VMhost_Name] -Server $NewvCenter
$NewNetwork = Get-VirtualPortGroup -VirtualSwitch 'vSwitch1' -Name [New_Network_Name] -VMhost $NewVMhost -Server $NewvCenter

There is quite a bit of configuration that needs to occur with the new vCenter. Luck for me that the environment had a flat network, and I didn’t have to target a bunch of different VLANs for the VMs. So, first you have to make a connection to the new vCenter and save it as a variable as you will need to use it for all of the other commands. First select the VMhost that will be used to transfer the VM on to, then select the datastore that you want the VM to be stored on once migrated. Lastly, you need to select the virtual portgroup that the VM will be attached. Making sure that when getting the VPG that you target the same VMhost as you already selected. If not, errors will be your friend.

$OldvCenter = Connect-VIServer [Old_vCenter_Name] -Credential $(Get-Credential)
$VM = Get-VM [VMName_to_Migrate] -Server $OldvCenter
$NetworkAdapter = Get-NetworkAdapter -VM $VM -Server $OldvCenter

There isn’t a lot to get from the old vCenter, other than the VM and its network adapter info.

$VM | Move-VM -Datastore $NewDatastore -NetworkAdapter $NetworkAdapter -PortGroup $NewNetwork -DiskStorageFormat Thin -Destination $NewVMHost

Now using the Move-VM cmdlet, you can target the data that was collected from the new vCenter section and move the VM. Please note, that you will need to disconnect any ISOs that you might have attached to the VM as they will not move and cause the migration to error out.

– Stuart

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.