@@ -156,21 +156,42 @@ function Get-AzMigrateHCIServerReplication {
156
156
$null = $PSBoundParameters.Add (' ResourceGroupName' , $ResourceGroupName )
157
157
$null = $PSBoundParameters.Add (' SiteName' , $siteName )
158
158
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 )) {
166
160
throw " Unknown machine site '$siteName ' with Type '$siteType '."
167
161
}
168
162
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
+ }
174
195
}
175
196
176
197
$null = $PSBoundParameters.Remove (' SiteName' )
0 commit comments