HCX PowerCLI and Reverse Migrations #HCX #PowerCLI @saintdle @Virtual_Simon


So when you are going to migrating a lot of VMs you really need to script it out, as doing it in the GUI is going to slow you down massively!

Now I have posted scripts where going forward to the cloud works well, but I found going in reverse can make the scripts a bit more complex. This is because the source and destination sites change, but you still do everything from the HCX Connector appliance.

Special shout out to Dean Lewis @saintdle and Simon Conyard @Virtual_Simon. I have enough PowerCLI skills to be dangerous but I was falling over and needed their assistance to help me figure out what was going on.

So this script will let you migrate from the cloud to legacy, and one issue I found is that the legacy side if it has complex vCenter folder structures HCX has trouble dealing with it. The way I was thinking of getting around this was to have a dumping folder and then move then post-migration using vSphere PowerCLI as a post-migration task.

But Dean and Simon had other ideas and managed to help me merge it into one script.

This script does bulk migrations, but it is pretty simple to change it to vMotion

write-host(“Getting Time for Scheduling”)
$startTime = [DateTime]::Now.AddDays(12)
$endTime = [DateTime]::Now.AddDays(15)
Connect-HCXServer -Server  (Legacy HCX Connector) # Connect to HCX Connector at Legacy 
Connect-VIServer -Server w(LegacyVC) # Connect to Legacy vCenter
write-host(“Getting Source Site”)

$HcxSrcSite = Get-HCXSite -Destination -server (HCX Connctor) -name  (Cloud VC) # Source site is the cloud

write-host(“Getting Target Site”)

$HcxDstSite = Get-HCXSite -Source (LegacyVC) # Destination is Legacy vCenter

$HCXVMS = Import-CSV -Path 'E:\PowerCLI\Bulk\FromCloudtoLegacy\HCX_Reverse_BULK_Migration_From_Cloud_to_Legacy_withCSV.csv'

ForEach ($HCXVM in $HCXVMS) {
    $folderfull = get-folder $HCXVM.DESTINATION_VM_FOLDER | where {$_.Parent -Match $HCXVM.DESTINATION_VM_ParentFOLDER} | select Id 
    $foldershort = $folderfull.Id.Replace("Folder-group-","*")
    $ContainerUid = Get-HCXContainer | where {$_.Id -Like $foldershort} | Select Uid
    $DstFolder = Get-hcxcontainer -Uid $ContainerUid.Uid
    $DstCompute = Get-HCXContainer -Type Cluster $HCXVM.DESTINATION_CLUSTER_OR_HOST  -Site $HcxDstSite
    $DstDatastore = Get-HCXDatastore $HCXVM.DESTINATION_DATASTORE -Site $HcxDstSite
    $SrcNetwork = Get-HCXNetwork $HCXVM.SOURCE_PORTGROUP -type DistributedVirtualPortgroup -Site $HcxSrcSite
    $DstNetwork = Get-HCXNetwork $HCXVM.DESTINATION_PORTGROUP -Type DistributedVirtualPortgroup -Site $HcxDstSite
    $NetworkMapping = New-HCXNetworkMapping -SourceNetwork $SrcNetwork -DestinationNetwork $DstNetwork
    $NewMigration = New-HCXMigration -VM (Get-HCXVM -name $HCXVM.VM_NAME -site $HcxSrcSite )  -MigrationType Bulk -SourceSite $HcxSrcSite -DestinationSite $HcxDstSite -Folder $DstFolder  -TargetComputeContainer $DstCompute -TargetDatastore $DstDatastore -NetworkMapping $NetworkMapping -DiskProvisionType Thin -UpgradeVMTools $False -RemoveISOs $True -ForcePowerOffVm $True -RetainMac $True -UpgradeHardware $False -RemoveSnapshots $True -ScheduleStartTime $startTime -ScheduleEndTime $endTime 
    Start-HCXMigration -Migration $NewMigration -Confirm:$false -WhatIf
    
    }
Disconnect-HCXServer -Server (HCX Connector at Legacy) -Confirm:$false
Disconnect-VIServer -Server (Legacy VC) -Confirm:$False

# This script is used for bulk migrations, you can populate the csv with VMs to migrate from Cloud to the Legacy VC/clusters
# The -WhatIf is used for testing, once you have populated the csv uncomment the -WhatIf and run it, if it runs without errors you can comment it out again and run the command for it to configure actual migrations 
# Bulk Syncs will start but the cut over is scheduled for 12 days in advance, this allows you to configure the syncs to start in advance of the migration event, and during the event you can cut over VMs by brinng the date forward to what suits you (in the web interface)
# Since the legacy side has multiple sub folders with the same name in vCenter, this script needs to connect to vcenter and pull the IDs for the correct folder based on its uid and then pass that back to hcx, since hcx does not support this directly,
# which is what the script is doing with #folderfull #foldershot #containeruid

The CSV file has the following bits listed :

CSV File Example

If you want to just do reverse migrations and sort the folders out later, you can have a simpler script as shown below:

write-host(“Getting Source Site”)

$HcxSrcSite = Get-HCXSite -Destination -server (Legacy HCX Connector) -name  (Cloud vCenter)

write-host(“Getting Target Site”)

$HcxDstSite = Get-HCXSite -Source (Legacy vCenter)
$HCXVMS = Import-CSV -Path 'E:\PowerCLI\HCX_Reverse Migration_From_Cloud_to_Legacy_withCSV.csv'

ForEach ($HCXVM in $HCXVMS) {
    $DstFolder = Get-HCXContainer $HCXVM.DESTINATION_VM_FOLDER -Site $HcxDstSite
    $DstCompute = Get-HCXContainer -Type Cluster $HCXVM.DESTINATION_CLUSTER_OR_HOST  -Site $HcxDstSite
    $DstDatastore = Get-HCXDatastore $HCXVM.DESTINATION_DATASTORE -Site $HcxDstSite
    $SrcNetwork = Get-HCXNetwork $HCXVM.SOURCE_PORTGROUP -type DistributedVirtualPortgroup -Site $HcxSrcSite
    $DstNetwork = Get-HCXNetwork $HCXVM.DESTINATION_PORTGROUP -Type DistributedVirtualPortgroup -Site $HcxDstSite
    $NetworkMapping = New-HCXNetworkMapping -SourceNetwork $SrcNetwork -DestinationNetwork $DstNetwork
    $NewMigration = New-HCXMigration -VM (Get-HCXVM -name $HCXVM.VM_NAME -site $HcxSrcSite )  -MigrationType vMotion -SourceSite $HcxSrcSite -DestinationSite $HcxDstSite -Folder $DstFolder -TargetComputeContainer $DstCompute -TargetDatastore $DstDatastore -NetworkMapping $NetworkMapping -DiskProvisionType Thin -UpgradeVMTools $False -RemoveISOs $True -ForcePowerOffVm $True -RetainMac $True -UpgradeHardware $False -RemoveSnapshots $True 
    Start-HCXMigration -Migration $NewMigration -Confirm:$false #-WhatIf
    }
Disconnect-HCXServer -Server (Legacy HCX Connector) -Confirm:$false

CSV File Example

Be the first to comment

Leave a Reply

Your email address will not be published.


*


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