Categories: Powershell
Retry loop
Post date:
Author: admin
Example on how you can create a Powershell loop in scripts using do/while and try/catch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $Stoploop = $false $retries = 3 [int]$Retrycount = 0 do { try { Scripts Commands here Write-Host -Object 'Job completed' $Stoploop = $true } catch { if ($Retrycount -eq $retries) { Write-Host -Object "Could not do the job after $retries retries" $Stoploop = $true } else { Write-Host -Object "Could not do the job, retrying in 30 seconds..." Start-Sleep -Seconds 30 $Retrycount++ } } } While ($Stoploop -eq $false) |