WilliamLam.com

  • About
    • About
    • Privacy
  • VMware Cloud
  • Tanzu
    • Application Modernization
    • Tanzu services
    • Tanzu Community Edition
    • Tanzu Kubernetes Grid
    • vSphere with Tanzu
  • Home Lab
  • Nested Virtualization
  • 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 // Leave a Comment

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
  • Quick demo videos of new VMware Cloud with Tanzu services

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

Thanks for the comment! Cancel reply

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

Search

Author

William Lam is a Senior Staff Solution Architect working in the VMware Cloud team within the Cloud Infrastructure Business Group (CIBG) at VMware. He focuses on Cloud Native technologies, Automation, Integration and Operation for the VMware Cloud based Software Defined Datacenters (SDDC)

Connect

  • Email
  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Vimeo

Recent

  • Automated ESXi Installation with a USB Network Adapter using Kickstart 02/01/2023
  • How to bootstrap ESXi compute only node and connect to vSAN HCI Mesh? 01/31/2023
  • Quick Tip - Easily move or copy VMs between two Free ESXi hosts? 01/30/2023
  • vSphere with Tanzu using Intel Arc GPU 01/26/2023
  • Quick Tip - Automating allowed and not allowed Datastores for use with vSphere Cluster Services (vCLS) 01/25/2023

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 © 2023

 

Loading Comments...