Skip to content

Commit 3a67c55

Browse files
Helen AfeworkSam Lee
authored andcommitted
Added retry on get machine site.
1 parent e52c4d4 commit 3a67c55

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/Migrate/Migrate.Autorest/custom/Get-AzMigrateHCIServerReplication.ps1

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,42 @@ function Get-AzMigrateHCIServerReplication {
156156
$null = $PSBoundParameters.Add('ResourceGroupName', $ResourceGroupName)
157157
$null = $PSBoundParameters.Add('SiteName', $siteName)
158158

159-
if ($siteType -eq $SiteTypes.VMwareSites) {
160-
$siteObject = Az.Migrate\Get-AzMigrateSite @PSBoundParameters -ErrorVariable notPresent -ErrorAction SilentlyContinue
161-
}
162-
elseif ($siteType -eq $SiteTypes.HyperVSites) {
163-
$siteObject = Az.Migrate.Internal\Get-AzMigrateHyperVSite @PSBoundParameters -ErrorVariable notPresent -ErrorAction SilentlyContinue
164-
}
165-
else {
159+
if (($siteType -ne $SiteTypes.HyperVSites) -and ($siteType -ne $SiteTypes.VMwareSites)) {
166160
throw "Unknown machine site '$siteName' with Type '$siteType'."
167161
}
168162

169-
if ($null -eq $siteObject) {
170-
throw "Machine site '$siteName' with Type '$siteType' not found."
171-
}
172-
else {
173-
$ProjectName = $siteObject.DiscoverySolutionId.Split("/")[8]
163+
# Occasionally, Get Machine Site will not return machine site even when the site exist,
164+
# hence retry get machine site.
165+
$attempts = 4
166+
for ($i = 1; $i -le $attempts; $i++) {
167+
try {
168+
if ($siteType -eq $SiteTypes.VMwareSites) {
169+
$siteObject = Az.Migrate\Get-AzMigrateSite @PSBoundParameters -ErrorVariable notPresent -ErrorAction SilentlyContinue
170+
}
171+
elseif ($siteType -eq $SiteTypes.HyperVSites) {
172+
$siteObject = Az.Migrate.Internal\Get-AzMigrateHyperVSite @PSBoundParameters -ErrorVariable notPresent -ErrorAction SilentlyContinue
173+
}
174+
175+
if ($null -eq $siteObject) {
176+
throw "Machine site not found."
177+
}
178+
else {
179+
$ProjectName = $siteObject.DiscoverySolutionId.Split("/")[8]
180+
}
181+
182+
break;
183+
}
184+
catch {
185+
if ($i -lt $attempts)
186+
{
187+
Write-Host "Machine site not found. Retrying in 30 seconds..."
188+
Start-Sleep -Seconds 30
189+
}
190+
else
191+
{
192+
throw "Machine site '$siteName' with Type '$siteType' not found."
193+
}
194+
}
174195
}
175196

176197
$null = $PSBoundParameters.Remove('SiteName')

0 commit comments

Comments
 (0)