From ccff4ad68a6c75ef5d129f47038d1e1108c3b959 Mon Sep 17 00:00:00 2001 From: Sam Lee Date: Wed, 21 May 2025 18:34:25 -0700 Subject: [PATCH 1/3] Fix ErrorAction duplication error --- .../custom/Helper/CommonHelper.ps1 | 31 +++++++++++++++++-- .../New-AzMigrateLocalServerReplication.ps1 | 11 ++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Migrate/Migrate.Autorest/custom/Helper/CommonHelper.ps1 b/src/Migrate/Migrate.Autorest/custom/Helper/CommonHelper.ps1 index a84c9520191f..5080a532ef6e 100644 --- a/src/Migrate/Migrate.Autorest/custom/Helper/CommonHelper.ps1 +++ b/src/Migrate/Migrate.Autorest/custom/Helper/CommonHelper.ps1 @@ -208,9 +208,35 @@ function InvokeAzMigrateGetCommandWithRetries { ) process { + # Filter out ErrorAction and ErrorVariable from the parameters + $params = @{} + foreach ($key in $Parameters.Keys) { + if ($key -ne "ErrorAction" -and $key -ne "ErrorVariable") { + $params[$key] = $Parameters[$key] + } + } + + # Extract user-specified ErrorAction and ErrorVariable or defaults + # but do not include them in $params + if ($Parameters.ContainsKey("ErrorVariable")) { + $errorVariable = $Parameters["ErrorVariable"] + } + else + { + $errorVariable = "notPresent" + } + + if ($Parameters.ContainsKey("ErrorAction")) { + $errorAction = $Parameters["ErrorAction"] + } + else + { + $errorAction = "Continue" + } + for ($i = 0; $i -le $MaxRetryCount; $i++) { try { - $result = & $CommandName @Parameters -ErrorVariable notPresent -ErrorAction SilentlyContinue + $result = & $CommandName @params -ErrorVariable $errorVariable -ErrorAction $errorAction if ($null -eq $result) { throw $ErrorMessage @@ -223,7 +249,7 @@ function InvokeAzMigrateGetCommandWithRetries { Start-Sleep -Seconds $RetryDelayInSeconds } else { - throw $ErrorMessage + throw "Get command failed after $MaxRetryCount retries. Error: $($_.Exception)" } } } @@ -231,6 +257,7 @@ function InvokeAzMigrateGetCommandWithRetries { return $result } } + function ValidateReplication { [Microsoft.Azure.PowerShell.Cmdlets.Migrate.DoNotExportAttribute()] param ( diff --git a/src/Migrate/Migrate.Autorest/custom/New-AzMigrateLocalServerReplication.ps1 b/src/Migrate/Migrate.Autorest/custom/New-AzMigrateLocalServerReplication.ps1 index 5e6abbe2d78e..8c87ed7e070c 100644 --- a/src/Migrate/Migrate.Autorest/custom/New-AzMigrateLocalServerReplication.ps1 +++ b/src/Migrate/Migrate.Autorest/custom/New-AzMigrateLocalServerReplication.ps1 @@ -188,19 +188,22 @@ function New-AzMigrateLocalServerReplication { $null = $PSBoundParameters.Remove('Confirm') $MachineIdArray = $MachineId.Split("/") + if ($MachineIdArray.Length -lt 11) { + throw "Invalid machine ID '$MachineId'" + } $SiteType = $MachineIdArray[7] $SiteName = $MachineIdArray[8] $ResourceGroupName = $MachineIdArray[4] $MachineName = $MachineIdArray[10] - - if (($SiteType -ne $SiteTypes.HyperVSites) -and ($SiteType -ne $SiteTypes.VMwareSites)) { - throw "Site type is not supported. Site type '$SiteType'. Check MachineId provided." - } # Get the source site and the discovered machine $null = $PSBoundParameters.Add("ResourceGroupName", $ResourceGroupName) $null = $PSBoundParameters.Add("SiteName", $SiteName) $null = $PSBoundParameters.Add("MachineName", $MachineName) + + if (($SiteType -ne $SiteTypes.HyperVSites) -and ($SiteType -ne $SiteTypes.VMwareSites)) { + throw "Site type is not supported. Site type '$SiteType'. Check MachineId provided." + } if ($SiteType -eq $SiteTypes.HyperVSites) { $instanceType = $AzLocalInstanceTypes.HyperVToAzLocal From 4f5baefd30a6cc60a1ea0f3269ad56866cea9e30 Mon Sep 17 00:00:00 2001 From: Sam Lee Date: Wed, 21 May 2025 18:40:09 -0700 Subject: [PATCH 2/3] Updated changelogs --- src/Migrate/Migrate/ChangeLog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Migrate/Migrate/ChangeLog.md b/src/Migrate/Migrate/ChangeLog.md index 80e9684371bc..9abcaba3b8e0 100644 --- a/src/Migrate/Migrate/ChangeLog.md +++ b/src/Migrate/Migrate/ChangeLog.md @@ -19,6 +19,7 @@ --> ## Upcoming Release * Implemented the Get-AzMigrateServerMigrationStatus cmdlet to retrieve the replication status of servers in Azure Migrate. +* Fixed bugs in `New-AzMigrateLocalServerReplication` that caused HyperVSite or VMwareSite not found. ## Version 2.7.1 From 27bd751b1f1d20ee337b8bb0e089e6e962724578 Mon Sep 17 00:00:00 2001 From: Sam Lee Date: Thu, 22 May 2025 14:02:29 -0700 Subject: [PATCH 3/3] Update error message --- .../custom/Get-AzMigrateLocalServerReplication.ps1 | 2 +- .../custom/New-AzMigrateLocalServerReplication.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Migrate/Migrate.Autorest/custom/Get-AzMigrateLocalServerReplication.ps1 b/src/Migrate/Migrate.Autorest/custom/Get-AzMigrateLocalServerReplication.ps1 index cf16e6d11956..126c1843be69 100644 --- a/src/Migrate/Migrate.Autorest/custom/Get-AzMigrateLocalServerReplication.ps1 +++ b/src/Migrate/Migrate.Autorest/custom/Get-AzMigrateLocalServerReplication.ps1 @@ -147,7 +147,7 @@ function Get-AzMigrateLocalServerReplication { if ($parameterSet -eq 'GetBySDSID') { $machineIdArray = $DiscoveredMachineId.Split("/") if ($machineIdArray.Length -lt 11) { - throw "Invalid machine ID '$DiscoveredMachineId'" + throw "Invalid machine ARM ID '$DiscoveredMachineId'" } $siteType = $machineIdArray[7] $siteName = $machineIdArray[8] diff --git a/src/Migrate/Migrate.Autorest/custom/New-AzMigrateLocalServerReplication.ps1 b/src/Migrate/Migrate.Autorest/custom/New-AzMigrateLocalServerReplication.ps1 index 8c87ed7e070c..54f8b315ff06 100644 --- a/src/Migrate/Migrate.Autorest/custom/New-AzMigrateLocalServerReplication.ps1 +++ b/src/Migrate/Migrate.Autorest/custom/New-AzMigrateLocalServerReplication.ps1 @@ -189,7 +189,7 @@ function New-AzMigrateLocalServerReplication { $MachineIdArray = $MachineId.Split("/") if ($MachineIdArray.Length -lt 11) { - throw "Invalid machine ID '$MachineId'" + throw "Invalid machine ARM ID '$MachineId'" } $SiteType = $MachineIdArray[7] $SiteName = $MachineIdArray[8]