VMware Cloud Community
Vel_VMware
Enthusiast
Enthusiast

VMware HCX migration using powerCLI

Hi

Can anyone help me to check the below script is anything wrong in it.. I am getting error while running. This is for VMware HCX migration powerCLI script.

I tried to initiate migration by using this script and but getting error.

$Data = Import-csv C:\Input.csv -delimiter “;”

$Data | foreach {

$VmHost = $_.ServerName

$SourceSite = $_.SourceSite

$DestSite = $_.DestinationSite

$DestFolder = $_.containerFolder

$DestinationCompute = $_.DestinationCompute

$Datastore = $_.DestinationDataStore

$Network = $_.DestinationNetwork

#####Intiating Miration

$newMigration = New-HCXMigration -DestinationSite $DestSite -DiskProvisionType Thick -Folder $DestFolder -MigrationType Bulk -SourceSite $SourceSite -TargetComputeContainer $DestinationCompute -TargetDatastore $Datastore -TargetNetwork $Network -VM $VmHost

Test-HCXMigration -Migration $newMigration

#Start-HCXMigration -Migration $newMigration -Confirm:$false

}

Thanks in advance

0 Kudos
27 Replies
LucD
Leadership
Leadership

And which error do you get?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

New-HCXMigration : Cannot bind parameter 'DestinationSite'. Cannot convert the

"10.159.170.103" value of type "System.String" to type "VMware.VimAutomation.Hc

x.Types.V1.HCXSite".

At C:\MigratioN.Ps1:12 char:51

+ $newMigration = New-HCXMigration -DestinationSite $DestSite -DiskProv ...

+                                                   ~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-HCXMigration], Paramet

   erBindingException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat

   ion.Hcx.Commands.Cmdlets.NewHCXMigration

Test-HCXMigration : Cannot bind argument to parameter 'Migration' because it is

null.

At C:\MigratioN.Ps1:.Ps1:13 char:30

+ Test-HCXMigration -Migration $newMigration

+                              ~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Test-HCXMigration], ParameterB

   indingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,V

   Mware.VimAutomation.Hcx.Commands.Cmdlets.TestHCXMigration

0 Kudos
LucD
Leadership
Leadership

The value on the Destination parameter needs to be an object returned by Get-HCXSite, not a string.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

I am not getting, can you please help me explaining bit more

0 Kudos
LucD
Leadership
Leadership

Something like this

$dest = Get-HCXSite -Name $DestSite

$newMigration = New-HCXMigration -DestinationSite $dest -DiskProvisionType Thick -Folder $DestFolder -MigrationType Bulk -SourceSite


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi LuCD,

Sorry to late to be back on this. I was trying to create the PowerCLI script to automate VMware HCX migration.. Unfortunately I couldn't do it successfully.

Can you help me with the script to automate the VMware HCX migration.

Thanks in advance.

0 Kudos
LucD
Leadership
Leadership

I'm afraid I don't have access to HCX hardware, so I can't really test anything.

Are you still getting the same error?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi LuCD.

Yeah, Some how am unable to fix this.. Also, I want to add few more feature also Which I really don't know how to do it.

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi LuCD,

If I give you the drafted code, is it possible for you to bug it if anything.

Thanks in advance.

0 Kudos
LucD
Leadership
Leadership

I can have a look at it, but without the specific HW it will be difficult to diagnose runtime errors.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi LuCD,

Good Day...!

I have created script and it works fine also.. Need your help to fine tune the script.

1) I want to create input popup to ask the HCX Server name since I entered the it directly in the script.

2) Create condition to show connection successful message only if connect established successfully.

3) Multiple network mapping upto 5 networks. Below script has only one network to map. If VM's has below 5 networks, script has to ignore and run rest of the network mapping.

4) You could New-HCXMigration will have the input for CSV file. So CSV file will have 100's of VM's. Test-HCXMigration performs subsequently and its moving to next server once migration initiated for previous server. This is taking too much time. Want to optimize it like Test-HCXMigration has to be done for all servers  and it has to generate the result report and save it in a path. Then, it has to initiate the Start-HCXMigration for all servers.

######## Script######

$Name= $Env:Username

$Cre = Get-Credential -Message "!!! Greetings $Name !!!"

$Data = Import-csv T:\Input.csv -delimiter “;”

$Server = "HCXServer"

$Newmigration = @()

Write-Host "Connecting to HCXServer:" $Server"..."

Connect-HCXServer -server $server -credential $Cre

Write-Host "Connection to HCXServer: "$Server" has been extablished successfully."

$Data | foreach {

$VmHost = $_.ServerName

$SourceSite = $_.SourceSite

$SourceNetwork = $_.SourceNetwork

$DestSite = $_.DestinationSite

$DestFolder = $_.containerFolder

$DestinationCompute = $_.DestinationCompute

$Datastore = $_.DestinationDataStore

$DestNetwork = $_.DestinationNetwork

$InitiateTime = $_.ScheduleStartTime

$CompleteTime = $_.ScheduleEndTime

write-host("Getting Time for Scheduling")

$StartTime = Get-Date $InitiateTime

$EndTime = Get-Date $CompleteTime

$SrcSite = Get-HCXSite -Source $SourceSite

$DstSite = Get-HCXSite -Destination $DestSite

$vm = Get-HCXVM -Name $VmHost -Site $SrcSite

$TargetCluster = Get-HCXContainer -Name $DestinationCompute -Site $DstSite

$TargetFolder = Get-HCXContainer -Name $DestFolder -Site $DstSite

$TargetDatastore = Get-HCXDatastore -Name $Datastore -Site $DstSite

$SrcNetwork = Get-HCXNetwork -Name $SourceNetwork -type DistributedVirtualPortgroup -Site $SrcSite

$DstNetwork = Get-HCXNetwork -Name $DestNetwork -type DistributedVirtualPortgroup -Site $DstSite

$TarNetwork = New-HCXNetworkMapping -SourceNetwork $SrcNetwork -DestinationNetwork $DstNetwork

#####Intiating Miration

ForEach($HCXVM in $vm)

{

Write-host("Adding workloads for migration...")

$newMigration = New-HCXMigration -SourceSite $SrcSite -DestinationSite $DstSite -VM $vm -MigrationType Bulk -DiskProvisionType Thick -TargetComputeContainer $TargetCluster -TargetDatastore $TargetDatastore -NetworkMapping $TarNetwork -UpgradeVMTools $False -RemoveISOs $True -ForcePowerOffVm $False -RetainMac $True -UpgradeHardware $False -RemoveSnapshots $False -ScheduleStartTime $StartTime -ScheduleEndTime $EndTime

Test-HCXMigration -Migration $newMigration | Export-CSV T:\Script\MigrationStatus.csv -NoTypeInformation -UseCulture

#$TestResult | Export-CSV T:\Script\MigrationTestResult.csv -NoTypeInformation -UseCulture

Start-HCXMigration -Migration $newMigration -Confirm:$true

}

#$MigrationStatus = Get-HCXMigration `| Format-List | Export-CSV T:\Script\MigrationStatus.csv -NoTypeInformation -UseCulture

#Write-host("Status report generated")

}

0 Kudos
LucD
Leadership
Leadership

1. You could use a Read-Host, like this

          $Server = Read-Host  -Prompt 'Enter HCX Server'

2. You could use a try-catch

try{

    Connect-HCXServer -server $server -credential $Cre -ErrorAction Stop

}

catch {

    throw "Connection to $Server failed!"

}

3. Not sure what you mean here.

4. You would still need a Test-HcxMigration call before eventually doing a Start-HcxMigration. I would suggest to only try a migration when the test returns success in ValidationResult.Type. If you want to run migrations in parallel, you would need to look at background jobs (with Start-Job).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

above script has capable to map only one target network and it defined as well New-HCXNetworkMapping. Few VM's has more than a NIC's and Portgroups, so how can I make the script to map multiple target portgroups with respective source portgroups.

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

No, I don't want to start the migration in parallel while validation happens. Normally this script executes and adds the servers one by one, validating and migrating.  To more clear, It takes one VM, testing it and initiating migration, then second VM, testing it  and initiation migration and then third VM's. This  is really very tedious to check the validation error in screen. So, want to run test validation first for entire servers in list and it will generate validation results. This result has to save in a path in CSV format with error and warnings. Once I had fix the error/Warnings from the validation result, let me run the script again and validation get succeeds and then migration starts.

0 Kudos
LucD
Leadership
Leadership

Like I said earlier, I don't have the HW to actually test anything.

But does the following list for each VM the networks to which is connected correctly?

Get-HCXVM -Name $VmHost -Site $SrcSite -PipelineVariable vm | %{

    $vm.Network |

    Select @{N='VM';E={$vm.Name}},Name,Type

}


If yes, you would need a table somewhere that lists which network is migrated to which new network.

Or are the network names the same on source and destination?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership

Then you probably better create two scripts, one for validation and another one (after you fix the errors) for the actual migration.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

No, Network names are different from source. How can I invoke the table values into the script.

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Ok, Can you help with that and also, I need to export validation result into CSV format in path.

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

$Name= $Env:Username

$Data = Import-csv T:\Script\Input.csv -delimiter “;”

$Server = Read-Host  -Prompt 'Enter HCX Server'

$Cre = Get-Credential -Message "!!! Greetings $Name !!!"

try{

    Connect-HCXServer -server $server -credential $Cre -ErrorAction Stop

}

catch {

    throw "Connection to $Server failed!"

}

$Newmigration = @()

$Data | foreach {

$VmHost = $_.ServerName

$SourceSite = $_.SourceSite

$SourceNetworkA = $_.SourceNetworkA

$SourceNetworkB = $_.SourceNetworkB

$SourceNetworkC = $_.SourceNetworkC

$SourceNetworkD = $_.SourceNetworkD

$DestSite = $_.DestinationSite

$DestFolder = $_.containerFolder

$DestinationCompute = $_.DestinationCompute

$Datastore = $_.DestinationDataStore

$DestNetworkA = $_.DestinationNetworkA

$DestNetworkB = $_.DestinationNetworkB

$DestNetworkC = $_.DestinationNetworkC

$DestNetworkD = $_.DestinationNetworkD

$InitiateTime = $_.ScheduleStartTime

$CompleteTime = $_.ScheduleEndTime

######CutOver Scheduling#########

$StartTime = Get-Date $InitiateTime

$EndTime = Get-Date $CompleteTime

Write-Host "Scheduled CutOver : StartTime -" $StartTime "and" "EndTime -" $EndTime

#####HCX Call######

$SrcSite = Get-HCXSite -Source $SourceSite

$DstSite = Get-HCXSite -Destination $DestSite

$vm = Get-HCXVM -Name $VmHost -Site $SrcSite

$TargetCluster = Get-HCXContainer -Name $DestinationCompute -Site $DstSite

$TargetFolder = Get-HCXContainer -Name $DestFolder -Site $DstSite

$TargetDatastore = Get-HCXDatastore -Name $Datastore -Site $DstSite

#####Network Mapping#######

$SrcNetworkA = Get-HCXNetwork -Name $SourceNetworkA -type DistributedVirtualPortgroup -Site $SrcSite

$DstNetworkA = Get-HCXNetwork -Name $DestNetworkA -type DistributedVirtualPortgroup -Site $DstSite

$TarNetworkA = New-HCXNetworkMapping -SourceNetwork $SrcNetworkA -DestinationNetwork $DstNetworkA

$SrcNetworkB = Get-HCXNetwork -Name $SourceNetworkB -type DistributedVirtualPortgroup -Site $SrcSite

$DstNetworkB = Get-HCXNetwork -Name $DestNetworkB -type DistributedVirtualPortgroup -Site $DstSite

$TarNetworkB = New-HCXNetworkMapping -SourceNetwork $SrcNetworkB -DestinationNetwork $DstNetworkB

$SrcNetworkC = Get-HCXNetwork -Name $SourceNetworkC -type DistributedVirtualPortgroup -Site $SrcSite

$DstNetworkC = Get-HCXNetwork -Name $DestNetworkC -type DistributedVirtualPortgroup -Site $DstSite

$TarNetworkC = New-HCXNetworkMapping -SourceNetwork $SrcNetworkC -DestinationNetwork $DstNetworkC

$SrcNetworkD = Get-HCXNetwork -Name $SourceNetworkD -type DistributedVirtualPortgroup -Site $SrcSite

$DstNetworkD = Get-HCXNetwork -Name $DestNetworkD -type DistributedVirtualPortgroup -Site $DstSite

$TarNetworkD = New-HCXNetworkMapping -SourceNetwork $SrcNetworkD -DestinationNetwork $DstNetworkD

#####Intiating Miration

ForEach($HCXVM in $vm)

{

Write-host("Adding workloads for migration...")

$newMigration = New-HCXMigration -SourceSite $SrcSite -DestinationSite $DstSite -VM $vm -MigrationType Bulk -DiskProvisionType Thick -TargetComputeContainer $TargetCluster -TargetDatastore $TargetDatastore -NetworkMapping $TarNetworkA, $TarNetworkB, $TarNetworkC, $TarNetworkD  -UpgradeVMTools $False -RemoveISOs $True -ForcePowerOffVm $False -RetainMac $True -UpgradeHardware $False -RemoveSnapshots $False -ScheduleStartTime $StartTime -ScheduleEndTime $EndTime

try

{

Test-HCXMigration -Migration $newMigration -ErrorAction:Inquire

Start-HCXMigration -Migration $newMigration -Confirm:$true

write-host ("Migration initiated successfully")

}

catch

{

throw "Migration cannot be initiated for $VM due to errors"

}

}

}

Error Message

New-HCXNetworkMapping : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.Hcx.Types.V1.HCXNetwork'

required by parameter 'SourceNetwork'. Specified method is not supported.

At T:\Script\Migration initiation.ps1:53 char:53

+ $TarNetworkA = New-HCXNetworkMapping -SourceNetwork $SrcNetworkA -DestinationNet ...

+                                                     ~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-HCXNetworkMapping], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.Hcx.Commands.Cmdlets.NewHCXNetworkMapping

New-HCXNetworkMapping : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.Hcx.Types.V1.HCXNetwork'

required by parameter 'SourceNetwork'. Specified method is not supported.

At T:\Script\Migration initiation.ps1:57 char:53

+ $TarNetworkB = New-HCXNetworkMapping -SourceNetwork $SrcNetworkB -DestinationNet ...

+                                                     ~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-HCXNetworkMapping], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.Hcx.Commands.Cmdlets.NewHCXNetworkMapping

New-HCXNetworkMapping : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.Hcx.Types.V1.HCXNetwork'

required by parameter 'SourceNetwork'. Specified method is not supported.

At T:\Script\Migration initiation.ps1:61 char:53

+ $TarNetworkC = New-HCXNetworkMapping -SourceNetwork $SrcNetworkC -DestinationNet ...

+                                                     ~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-HCXNetworkMapping], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.Hcx.Commands.Cmdlets.NewHCXNetworkMapping

New-HCXNetworkMapping : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.Hcx.Types.V1.HCXNetwork'

required by parameter 'SourceNetwork'. Specified method is not supported.

At T:\Script\Migration initiation.ps1:65 char:53

+ $TarNetworkD = New-HCXNetworkMapping -SourceNetwork $SrcNetworkD -DestinationNet ...

+                                                     ~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-HCXNetworkMapping], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.Hcx.Commands.Cmdlets.NewHCXNetworkMapping

I am not sure where I am missing, Can you check...?

0 Kudos