Powershell, HCX – create mobility group based on VM list

  • By :
  • Category : VMware

However HCX is a fantastic migration tool, but when it comes to migrate lots of VM at once, it is important properly plan migration and in addition to create sufficient automation to fit in the migration (downtime) window.

One of the step that need to be created during preparation is to create mobility group(s) and validate migration if it is even possible.

Below you can find powershell script witch can be used to create mobility group. Script is quite easy and can be modified in a way will work for you. Below the script I am explaining some aspect of logic included in HCX powercli module. Code will create new mobility group, in addition will do the migration test and record the result into the text file:

The code can be used at your own risk.

#source site 
$sourceSite = get-hcxsite -Source -Name VCENTER -ErrorAction Ignore
#destination site
$destination= Get-HCXSite -Destination | where {$_.Uid -like "*IP_ADDRESS*"}

#read source vm list
$vms = Get-Content .\Desktop\list1.txt
#vm verification, if can be found on the source
foreach ($vm in $vms){
  if (-not (get-hcxvm -name $vm -Site $sourceSite)) {
    write-host $vm is not presented in $sourceSite.Name }
}

#set the destination objects for storage, RP and vm folder
$targetDatastore = Get-HCXDatastore -Site $destination -Name "vsanDatastore"
$targetContainer = Get-HCXContainer -Site $destination -Type ResourcePool -Name "RP-DESTINATION"
$targetFolder = Get-HCXContainer -Site $destination -Type Folder -Name "FOLDER-DESTINATION"

#destination network for the network mapping
$targetNetwork = Get-HCXNetwork -Type NsxtSegment -Name "DESTINATION_NETWORK" -Site $destination

#creating networkmapping and hcxmigration
$newMigrations = @()
foreach ($vm in $vms){
  $netMapping = @()
  $vm = Get-HCXVM $vm
  foreach ($net in $vm.Network){
    $netMapping += New-HCXNetworkMapping -SourceNetwork $net -DestinationNetwork $targetNetwork }
  write-host "Working on VM:" $vm.name "; Network mapping:" $netMapping
  $newMigrations += New-HCXMigration -DestinationSite $destination -DiskProvisionType Thin -ForcePowerOffVm $false -MigrationType RAV -RemoveSnapshots $true -RemoveISOs $true -RetainMac $true -MigrateCustomAttributes $true -SourceSite $sourceSite -TargetComputeContainer $targetContainer -TargetDatastore $targetDatastore -VM $vm -NetworkMapping $netMapping -MobilityGroupMigration 
}

$newGC = New-HCXMobilityGroupConfiguration -SourceSite $sourceSite -DestinationSite $destination
$mobilityGroup1 = New-HCXMobilityGroup -Name test1 -Migration $newMigrations -GroupConfiguration $newGC

$testMigration = Test-HCXMigration -Migration $newMigrations | select ValidationResult | ft -AutoSize
$testMigration | Out-File -FilePath .\Desktop\testMigration.txt
  • source site – VCENTER name where the source VM are
  • if you have more then one destination for some scenario it is not easy to find the correct one (where the HCX relationship has been configured). For that reason verify the destination – by UID and searching the correct source IP address in here
  • #vm verification is the code to verify if VMs taken from the list1.txt file are visible in the source vCenter
  • set the destination objects for storage, RP and vm folder block is self explanatory, but it is important to set proper, already existing objects
  • destination network for network mapping is configured as the only network for all source network. It is valid as an destination network that is the result of L2 network extension as well as network created manually
  • #creating networkmapping and hcxmigration – this is the key fragment of code, as HCXMigration object is critical to create new MobilityGroup (New-HCXMobilityGroupConfiguration function).
    Please pay attention to all parameters that goes to the New-HCXNetworkMapping, especially type of the migration. That function takes NetworkMapping argument as a array of HCXNetworkMapping defined in the prior loop (foreach ($net in $vm.Network). This is by the fact that HCX supports VM with more than one network card (starting from version 4.5)
  • HCXMobilityGroupConfiguration takes just two arguments, source and destination, and is required argument for New-HCXMobilityGroup function.
  • New-HCXMobilityGroup finally create mobility group (draft). It doesn’t run the group, it just make a new definition in HCX migration tab
  • finally Test-HCXMigration is verifying if VM can be migrated within the migration plan for all VM and possibly display the potential migration issue

Potential downsides:

  • testing process (as well as network mapping or vm verification) can take several minutes, be patient
  • be very careful when selecting source and destination, it needs to be the same as in HCX configuration (especially if you have more than one HCX pair)
  • destination entity like storage, resourcePool or vm folder needs to exists before you run the script

The code can be used at your own risk.

Source:

[1] https://gist.github.com/Zsoldier/feef3ca5d8d221bc5c469363ddbaf7e3

[2] https://developer.vmware.com/docs/powercli/latest/vmware.vimautomation.hcx/structures/vmware.vimautomation.hcx.types.v1.hcxnetworkmapping/

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Free(BSD)
Search for duplicated files

This will be short (but hopefully I will find more time to show entire process to search duplicated files together with some examples). In case you are searching for duplicated files I can recommend two software which actually rocks in openSource world

Azure
NFS issue, cannot be mounted or is not visible

The same kind of issue I have encountered numerous times while working across different environments and with various customers. The problem with NFS mounts connected from remote locations is so common. This issue extends beyond communication solely over WAN and also include connections between datacenters (DC) where we lack control …

Azure
Why Firefox is important and people should use this browser in 2024, my thoughts.

Can you remember the times when everyone was using Internet Explorer? Back in the ’90s and the early part of this century, Internet Explorer dominated the browser market. Software Incompatibility with Other Browsers Incompatibility issues with software and other browsers have been a persistent problem. Even in 2022, this remains …