Skip to content

Instantly share code, notes, and snippets.

@lamw
Created October 21, 2020 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lamw/c763dfb55b05fc7af71d0215aa98f794 to your computer and use it in GitHub Desktop.
Save lamw/c763dfb55b05fc7af71d0215aa98f794 to your computer and use it in GitHub Desktop.
PowerCLI example to create HCX Mobility Group based on vSphere Tags
$VC_SERVER="vcsa.vmware.corp"
$VC_USERNAME="administrator@vsphere.local"
$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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment