From cd1c004f4bdedb843189fa9bc02f236cac09362c Mon Sep 17 00:00:00 2001 From: Tomas Kracmar Date: Tue, 14 Apr 2026 18:58:20 +0200 Subject: [PATCH] fix(rename): add retry logic for transient 5xx/429 errors Settings Catalog and other Graph endpoints occasionally return 500 InternalServerError on PATCH. Retry up to 3 times with a 2s delay to improve reliability for bulk rename operations. --- Scripts/Bulk-RenamePolicies.ps1 | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) 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