HCX Migration PowerCLI Scripts #HCX @VMwareHCX #PowerCLI #vExpert


So I have been spending more and more time working with HCX and the web interface for migrations is all well and good….but really when you are wanting to do bulk migrations you are really going to want to script it

Now these scripts aren’t totally my own, I have butchered other peoples and amended them for my own use cases. The guys working at Global Migration Center (GMC) helped and they actually used William Lam scripts as a base point and a couple of other PSO engineers I work with added their bits too….so credit where credit is due 🙂

Script 1

write-host("Getting Time for Scheduling")
$startTime = [DateTime]::Now.AddDays(12)
$endTime = [DateTime]::Now.AddDays(15)

#Connect-HCXServer -Server  HCXSOURCESERVER
write-host("Getting Source Site")
$HcxSrcSite = get-hcxsite -source -server HCXSOURCESERVER
write-host("Getting Target Site")
$HcxDstSite = get-hcxsite -destination -server HCXSOURCESERVER -name "Destination VC"
write-host("Getting VM (This may take a while)")
$HcxVMS = Get-HCXVM -Name * -server HCXSOURCESERVER -site $HcxSrcSite  | Where-Object -Property Name -Like "*HCX*" #| Select-Object -Property Name
write-host("Getting Folder")
$DstFolder = Get-HCXContainer -Name "Dest VC Folder" -Site $HcxDstSite
write-host("Getting Container")
$DstCompute = Get-HCXContainer -Name "ESXIHOSTNAME or Dest Cluster"  -Site $HcxDstSite
write-host("Getting Datastore")
$DstDatastore = Get-HCXDatastore -Name "Dest Datastore" -Site $HcxDstSite
write-host("Getting Source Network")
$SrcNetwork = Get-HCXNetwork -Name "Source PortGroup" -type DistributedVirtualPortgroup -Site $HcxSrcSite
write-host("Getting Target Network")
$DstNetwork = Get-HCXNetwork -Name "Destination Network" -type DistributedVirtualPortgroup -Site $HcxDstSite
write-host("Creating Network Mapping")
$NetworkMapping = New-HCXNetworkMapping -SourceNetwork $SrcNetwork -DestinationNetwork $DstNetwork

write-host("Creating Migration Command")

ForEach ($HCXVM in $HCXVMS) {

$NewMigration = New-HCXMigration -VM $HcxVM -MigrationType Bulk -SourceSite $HcxSrcSite -DestinationSite $HcxDstSite -Folder $DstFolder `
-TargetComputeContainer $DstCompute -TargetDatastore $DstDatastore -NetworkMapping $NetworkMapping -DiskProvisionType Thin `
-UpgradeVMTools $True -RemoveISOs $True -ForcePowerOffVm $True -RetainMac $True -UpgradeHardware $True `
-RemoveSnapshots $True -ScheduleStartTime $startTime -ScheduleEndTime $endTime
Start-HCXMigration -Migration $NewMigration -Confirm:$false 
}

Now with the above script, you have to put in:

  • The HCX Enterprise Manager as the source
  • The Destination vCenter
  • The VMs, in my example I had some VMs that started with the name HCX so I filtered Get-HCXVM to find VM names that had “*HCX*” in them
  • The Destination vCenter Folder
  • The Destination ESXi host or Destination Cluster
  • Destination Datastore
  • The Source Portgroup
  • The Destination Port Group

Also at the very start, you see for the bulk migration I have set the start time 12 days from the current date, this means once the initial sync is done, it will keep replicating in the background and won’t cut over for 12 days. This is good because even though I have the intention to cut over much before then, I can force the cutover whenever I am ready but it will keep syncing till I do

Finally, there is a foreach, so that every VM listed in Get-HCXVM is configured the same. Now in this example, there are all coming from the same network and going to the same destination network. We are also telling HCX to Upgrade VMware Tools, Remove any attached ISOs, for the VM to Power Off if it hasn’t got VMware tools and to remove any snapshots

Script 2

Now with the script below all I have done is use Get-Content to use a list of VMs in a txt file. They are all in the same network and all going to the same destination network. So a nice simple variation of the above:

write-host("Getting Time for Scheduling")
$startTime = [DateTime]::Now.AddDays(12)
$endTime = [DateTime]::Now.AddDays(15)
 
#Connect-HCXServer -Server  HCXSOURCESERVER
write-host("Getting Source Site")
$HcxSrcSite = get-hcxsite -source -server HCXSOURCESERVER
write-host("Getting Target Site")
$HcxDstSite = get-hcxsite -destination -server HCXSOURCESERVER -name "Destination VC"
write-host("Getting VM (This may take a while)")
$HcxVMS = Get-HCXVM -Name (Get-Content "location of textfile") -server HCXSOURCESERVER -site $HcxSrcSite
write-host("Getting Folder")
$DstFolder = Get-HCXContainer -Name "Dest VC Folder" -Site $HcxDstSite
write-host("Getting Container")
$DstCompute = Get-HCXContainer -Name "ESXIHOSTNAME or Dest Cluster"  -Site $HcxDstSite
write-host("Getting Datastore")
$DstDatastore = Get-HCXDatastore -Name "Dest Datastore" -Site $HcxDstSite
write-host("Getting Source Network")
$SrcNetwork = Get-HCXNetwork -Name "Source PortGroup" -type DistributedVirtualPortgroup -Site $HcxSrcSite
write-host("Getting Target Network")
$DstNetwork = Get-HCXNetwork -Name "Destination Network" -type DistributedVirtualPortgroup -Site $HcxDstSite
write-host("Creating Network Mapping")
$NetworkMapping = New-HCXNetworkMapping -SourceNetwork $SrcNetwork -DestinationNetwork $DstNetwork
 
write-host("Creating Migration Command")
 
ForEach ($HCXVM in $HCXVMS) {
 
$NewMigration = New-HCXMigration -VM $HcxVM -MigrationType Bulk -SourceSite $HcxSrcSite -DestinationSite $HcxDstSite -Folder $DstFolder `
-TargetComputeContainer $DstCompute -TargetDatastore $DstDatastore -NetworkMapping $NetworkMapping -DiskProvisionType Thin `
-UpgradeVMTools $True -RemoveISOs $True -ForcePowerOffVm $True -RetainMac $True -UpgradeHardware $True `
-RemoveSnapshots $True -ScheduleStartTime $startTime -ScheduleEndTime $endTime
Start-HCXMigration -Migration $NewMigration -Confirm:$false
}

The text file is just made up of VM names listed one after another:

HCXTestlinux_prd
HCXTestlinux_dev
HCXTestwin_prd
HCXTestwin_dev

Script 3

Now the script below pulls details from a csv file and supports variations at the destination:

write-host(“Getting Time for Scheduling”)
$startTime = [DateTime]::Now.AddDays(12)
$endTime = [DateTime]::Now.AddDays(15)
Connect-HCXServer -Server  SOURCEHCXSERVER
write-host(“Getting Source Site”)
$HcxSrcSite = Get-HCXSite -Source “SourceSite”
write-host(“Getting Target Site”)
$HcxDstSite = Get-HCXSite -Destination “DestinationSite”
$HCXVMS = Import-CSV .\Import_VM_list.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 -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 $True -RemoveISOs $True -ForcePowerOffVm $True -RetainMac $True -UpgradeHardware $True -RemoveSnapshots $True -ScheduleStartTime $startTime -ScheduleEndTime $endTime
    Start-HCXMigration -Migration $NewMigration -Confirm:$false
}

The csv contains looks like this:

Just create the CSV and populate it with the info you need and you can migrate away!

These are all variations that accomplish the same thing, use whatever works for you and if you do anything funky with them and make some cool adjustments please let me know!


8 Comments

  1. Hi vmusketeers,
    Thanks for this article.
    My customer has to try more than 1000 VMs migration from on premise vSphere to VMC.
    I thought PowerCLI is good for them.
    I searched article for that, and found here.

    I confirmed it worked!!!

    Surprised at the article wrote more than 1 years ago.

    Thanks vmuketeers team!

  2. hello,
    thank you for this script.
    i have a question:
    How can i change Vm IP,DNS and Gateway using this script? i can put informations in CSV file.

1 Trackback / Pingback

  1. HCX Mobility Group Migrations using PowerCLI Scripts #HCX @VMwareHCX #PowerCLI #vExpert @saintdle -

Leave a Reply

Your email address will not be published.


*


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