Nothing posted here is supported by VMware 🙂
So when the original scripts were created, HCX Mobility Groups weren’t even a thing!
HCX evolves at a rapid rate and I was working with a customer who was using the original scripts but since they were migrating into VMware Cloud on AWS they had access to the Mobility Groups features!
The original scripts can be found here.
When working on these scripts with my buddy @saintdle we found that the way people had started using them didn’t work as expected for us, we tried William Lam’s script and we received errors:
We tried this as well and came across similar issues:
Also, the customer when trying the scripts came across the same issues.
Here is the script we came up with, it’s unsupported and in some areas quite dirty, it does error but the actions complete exactly as expected.
write-host(“Getting Time for Scheduling”)
$startTime = [DateTime]::Now.AddDays(12)
$endTime = [DateTime]::Now.AddDays(15)
Connect-HCXServer -Server "HCX CONNECTOR"
write-host(“Getting Source Site”)
$HcxSrcSite = Get-HCXSite -Source
write-host(“Getting Target Site”)
$HcxDstSite = Get-HCXSite -Destination “DEST SITE” -ErrorAction SilentlyContinue
write-host(“Define Mobility Group Source and Destination Sites”)
$NewMGC = New-HCXMobilityGroupConfiguration -SourceSite $HcxSrcSite -DestinationSite $HcxDstSite
write-host(“Define Mobility Group Name”)
$MobilityGroupName = "Batman"
write-host(“Import csv details for the VMs to be migrated”)
$HCXVMS = Import-CSV .\Import_VM_list_mobility.csv
ForEach ($HCXVM in $HCXVMS) {
$DstFolder = Get-HCXContainer $HCXVM.DESTINATION_VM_FOLDER -Site $HcxDstSite
$DstCompute = Get-HCXContainer $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 NsxtSegment -Site $HcxDstSite
$NetworkMapping = New-HCXNetworkMapping -SourceNetwork $SrcNetwork -DestinationNetwork $DstNetwork
$NewMigration = New-HCXMigration -VM (Get-HCXVM $HCXVM.VM_NAME) -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 -MobilityGroupMigration
#This is used the first time to create the mobility group with the required configuration, after the first go it will error saying its already created which is expected
$mobilityGroup1 = New-HCXMobilityGroup -Name $MobilityGroupName -Migration $NewMigration -GroupConfiguration $NewMGC -ErrorAction SilentlyContinue
#Even though this command will error with "Value cannot be null", it still works and adds each vm to the created mobility group
Set-HCXMobilityGroup -MobilityGroup (get-hcxmobilitygroup -name $MobilityGroupName) -Migration $NewMigration -addMigration -ErrorAction SilentlyContinue
}
#This will error with "Value cannot be null" but it will start the migrations of the VMs in the mobility group
Start-HCXMobilityGroupMigration -MobilityGroup (get-hcxmobilitygroup -name $MobilityGroupName) -ErrorAction SilentlyContinue
CSV:
I will be logging a PR internally to see if there has been some change with the latest updates to PowerCLI to cause these errors, or if we were doing something wrong!
I hope this helps people get on track with using HCX Mobility Groups!
can we add EXTENDED OPTIONS in a script like “Migrate custom attribute” also kindly share if you have a dedicated script for a CUTOVER
thanks in advance !!
as far as I am aware that is not possible at the moment
I already blogged about cut over
https://vmusketeers.com/2021/11/15/automating-hcx-cutover-using-powercli-hcx-powercli-vexpert/
Hello Bilal Ahmed,
Thanks for script and TAM Lab session on the script
I have few errors on running above script but GUI migration working fine