diff --git a/Scripts/Bulk-RenamePolicies.ps1 b/Scripts/Bulk-RenamePolicies.ps1 index 961881e..e4e94df 100644 --- a/Scripts/Bulk-RenamePolicies.ps1 +++ b/Scripts/Bulk-RenamePolicies.ps1 @@ -405,9 +405,34 @@ foreach($change in $changes) else { $body = $payload | ConvertTo-Json -Depth 10 -Compress - $null = Invoke-GraphRequest "$($objectType.API)/$($obj.id)" -HttpMethod PATCH -Content $body - Write-Host " OK: Renamed '$($change.OldName)' -> '$($change.NewName)'" -ForegroundColor Green - $success++ + $maxRetries = 3 + $retryDelay = 2 + $renamed = $false + for($r = 1; $r -le $maxRetries; $r++) + { + try + { + $null = Invoke-GraphRequest "$($objectType.API)/$($obj.id)" -HttpMethod PATCH -Content $body + Write-Host " OK: Renamed '$($change.OldName)' -> '$($change.NewName)'" -ForegroundColor Green + $success++ + $renamed = $true + break + } + catch + { + $statusCode = $_.Exception.Response.StatusCode + if($r -lt $maxRetries -and ($statusCode -ge 500 -or $statusCode -eq 429)) + { + Write-Host " Retry $r/$maxRetries after $retryDelay`s (HTTP $statusCode)..." -ForegroundColor DarkYellow + Start-Sleep -Seconds $retryDelay + } + else + { + throw + } + } + } + if(-not $renamed) { throw "Rename failed after $maxRetries attempts." } } } catch