WilliamLam.com

  • About
    • About
    • Privacy
  • VMware Cloud Foundation
  • VKS
  • Homelab
    • Resources
    • Nested Virtualization
  • VMware Nostalgia
  • Apple
You are here: Home / Automation / Using PowerCLI and vSphere Tags to create/migrate HCX Mobility Groups to VMware Cloud SDDC

Using PowerCLI and vSphere Tags to create/migrate HCX Mobility Groups to VMware Cloud SDDC

10.21.2020 by William Lam // 3 Comments

If using your voice to create an HCX Mobility Group and initiate a migration to a VMware Cloud SDDC is not your thing, here is a more practical example using PowerCLI which includes HCX cmdlets that was introduced awhile back.


Here are the 12 configurable variables that you will need to update based on your own environment.

$VC_SERVER="vcsa.vmware.corp"
$VC_USERNAME="*protected email*"
$VC_PASSWORD="VMware1!"
$HCX_SERVER="hcx.vmware.corp"

$VSPHERE_TAG_CATEGORY="Cloud"
$VSPHERE_TAG_NAME="VMC"

# vMotion, Bulk, Cold, RAV, OsAssistedMigration
$MIGRATION_TYPE="RAV"

$TARGET_NETWORK_NAME="L2E_HOL-10-f58e483b"
$TARGET_DATASTORE_NAME="WorkloadDatastore"
$TARGET_RESOURCE_POOL_NAME="Compute-ResourcePool"
$TARGET_VM_FOLDER_NAME="Workloads"

$MOBILITY_GROUP_NAME="VMworld-2020-Demo"

Next, we will connect to both our vCenter Server and HCX Manager endpoint to retrieve the VM details required to construct our HCX Mobility Group:

Connect-VIServer -Server $VC_SERVER -User $VC_USERNAME -Password $VC_PASSWORD -Force | Out-Null
Connect-HCXServer -Server $HCX_SERVER -User $VC_USERNAME -Password $VC_PASSWORD | Out-Null

This is line describes how we are filtering out the list of VMs. In this example, we are using vSphere Tags BUT, you can certainly filtering on almost any attribute you desire. This is just one very specific example and you can update the filtering logic to other parameters to create the $vms array list.

$vms = (Get-TagAssignment -Category $VSPHERE_TAG_CATEGORY | where {$_.Tag.Name -eq $VSPHERE_TAG_NAME}).Entity

This next section creates the HCX mappings between the source and destination vSphere endpoints:

$targetDatastore = Get-HCXDatastore -Site $targetSite -Name $TARGET_DATASTORE_NAME
$targetContainer = Get-HCXContainer -Site $targetSite -Type "ResourcePool" -Name $TARGET_RESOURCE_POOL_NAME
$targetFolder = Get-HCXContainer -Site $targetSite -Type Folder -Name $TARGET_VM_FOLDER_NAME

$sourceSite = Get-HCXSite -Source
$targetSite = Get-HCXSite -Destination

$mobilityGroupConfig = New-HCXMobilityGroupConfiguration -SourceSite $sourceSite -DestinationSite $targetSite

Now we construct our HCX Migration configuration which is based on the list of VMs we filtered out earlier. In most examples you find online, it only demonstrates how to do this with a single VM which is interesting but not that useful if you are looking to migrate multiple VMs and this is where HCX Mobility Group shines.

$hcxMigrations = @()
foreach ($vm in $vms) {
    $hcxVm = Get-HCXVM -Name $VM.Name

    $sourceNetwork = $hcxVm.Network[0]
    $targetNetwork = Get-HCXNetwork -Type NsxtSegment -Name $TARGET_NETWORK_NAME -Site $targetSite
    $networkMapping = New-HCXNetworkMapping -SourceNetwork $sourceNetwork -DestinationNetwork $targetNetwork

    $hcxMigration = New-HCXMigration -VM $hcxVm `
        -MigrationType $MIGRATION_TYPE `
        -SourceSite $sourceSite `
        -DestinationSite $targetSite `
        -DiskProvisionType SameAsSource `
        -RetainMac $true `
        -TargetComputeContainer $targetContainer `
        -TargetDatastore $targetDatastore `
        -NetworkMapping $networkMapping `
        -Folder $targetFolder `
        -MobilityGroupMigration

    $hcxMigrations += $hcxMigration
}

After we have our HCX Migration configuration stored in $hcxMigrations variable, we then create our HCX Mobility Group:

$mobilityGroup = New-HCXMobilityGroup -Name $MOBILITY_GROUP_NAME -Migration $hcxMigrations -GroupConfiguration $mobilityGroupConfig

Before initiating a migration with HCX Mobility Group, we can actually test and validate our configuration. To do so, simply use Test-HCXMobiltyGroup cmdlet:

Test-HCXMobilityGroup -MobilityGroup $mobilityGroup

Finally, start the migration, use the Start-HCXMobilityGroupMigration cmdlet:

Start-HCXMobilityGroupMigration -MobilityGroup $mobilityGroup

Here is a copy of the entire script for those that just want to copy/paste:


$VC_SERVER="vcsa.vmware.corp"
$VC_USERNAME="*protected email*"
$VC_PASSWORD="VMware1!"
$HCX_SERVER="hcx.vmware.corp"
$VSPHERE_TAG_CATEGORY="Cloud"
$VSPHERE_TAG_NAME="VMC"
# vMotion, Bulk, Cold, RAV, OsAssistedMigration
$MIGRATION_TYPE="RAV"
$TARGET_NETWORK_NAME="L2E_HOL-10-f58e483b"
$TARGET_DATASTORE_NAME="WorkloadDatastore"
$TARGET_RESOURCE_POOL_NAME="Compute-ResourcePool"
$TARGET_VM_FOLDER_NAME="Workloads"
$MOBILITY_GROUP_NAME="VMworld-2020-Demo"
Connect-VIServer -Server $VC_SERVER -User $VC_USERNAME -Password $VC_PASSWORD -Force | Out-Null
Connect-HCXServer -Server $HCX_SERVER -User $VC_USERNAME -Password $VC_PASSWORD | Out-Null
$vms = (Get-TagAssignment -Category $VSPHERE_TAG_CATEGORY | where {$_.Tag.Name -eq $VSPHERE_TAG_NAME}).Entity
$targetDatastore = Get-HCXDatastore -Site $targetSite -Name $TARGET_DATASTORE_NAME
$targetContainer = Get-HCXContainer -Site $targetSite -Type "ResourcePool" -Name $TARGET_RESOURCE_POOL_NAME
$targetFolder = Get-HCXContainer -Site $targetSite -Type Folder -Name $TARGET_VM_FOLDER_NAME
$sourceSite = Get-HCXSite -Source
$targetSite = Get-HCXSite -Destination
$mobilityGroupConfig = New-HCXMobilityGroupConfiguration -SourceSite $sourceSite -DestinationSite $targetSite
$hcxMigrations = @()
foreach ($vm in $vms) {
$hcxVm = Get-HCXVM -Name $VM.Name
$sourceNetwork = $hcxVm.Network[0]
$targetNetwork = Get-HCXNetwork -Type NsxtSegment -Name $TARGET_NETWORK_NAME -Site $targetSite
$networkMapping = New-HCXNetworkMapping -SourceNetwork $sourceNetwork -DestinationNetwork $targetNetwork
$hcxMigration = New-HCXMigration -VM $hcxVm `
-MigrationType $MIGRATION_TYPE `
-SourceSite $sourceSite `
-DestinationSite $targetSite `
-DiskProvisionType SameAsSource `
-RetainMac $true `
-TargetComputeContainer $targetContainer `
-TargetDatastore $targetDatastore `
-NetworkMapping $networkMapping `
-Folder $targetFolder `
-MobilityGroupMigration
$hcxMigrations += $hcxMigration
}
$mobilityGroup = New-HCXMobilityGroup -Name $MOBILITY_GROUP_NAME -Migration $hcxMigrations -GroupConfiguration $mobilityGroupConfig
Test-HCXMobilityGroup -MobilityGroup $mobilityGroup
Start-HCXMobilityGroupMigration -MobilityGroup $mobilityGroup
Disconnect-VIServer * -Confirm:$false | Out-Nul

view raw

gistfile1.txt

hosted with ❤ by GitHub

More from my site

  • Automating HCX Multi-Site Service Mesh configuration using the new HCX PowerCLI cmdlets
  • Automating complete HCX deployment and configuration to first cloud migration using PowerCLI
  • Automating Hybrid Cloud Extension (HCX) Manager initial configuration for VMC
  • Getting started with the Hybrid Cloud Extension (HCX) APIs
  • VMware Cloud (VMC) Console Inventory with various vSphere "Linked Modes"

Categories // Automation, Azure VMware Solution, Google Cloud VMware Engine, Oracle Cloud VMware Solution, PowerCLI, VMware Cloud, VMware Cloud on AWS Tags // HCX, Mobility Group, PowerCLI, tag, VMware Cloud, VMware Cloud on AWS

Comments

  1. *protectedBrad Hendrickson says

    04/24/2024 at 9:02 am

    Is it possible to specify a specific service mesh for each mobility group? To spread the load around?

    Reply
    • *protectedBrad Hendrickson says

      08/06/2024 at 4:41 pm

      Any ideas on how to accomplish this?

      Reply
  2. *protectedMOMO says

    09/04/2024 at 3:39 am

    after executing the script the service mesh showing not available. and this is actually blocking the sync. can you help with this? thank you.

    Reply

Thanks for the comment!Cancel reply

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

Search

Thank Author

Author

William is Distinguished Platform Engineering Architect in the VMware Cloud Foundation (VCF) Division at Broadcom. His primary focus is helping customers and partners build, run and operate a modern Private Cloud using the VMware Cloud Foundation (VCF) platform.

Connect

  • Bluesky
  • Email
  • GitHub
  • LinkedIn
  • Mastodon
  • Reddit
  • RSS
  • Twitter
  • Vimeo

Recent

  • VMware Flings is now available in Free Downloads of Broadcom Support Portal (BSP) 05/19/2025
  • VMUG Connect 2025 - Minimal VMware Cloud Foundation (VCF) 5.x in a Box  05/15/2025
  • Programmatically accessing the Broadcom Compatibility Guide (BCG) 05/06/2025
  • Quick Tip - Validating Broadcom Download Token  05/01/2025
  • Supported chipsets for the USB Network Native Driver for ESXi Fling 04/23/2025

Advertisment

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright WilliamLam.com © 2025

 

Loading Comments...