Created
December 6, 2021 19:44
-
-
Save jcefoli/42a01a4681d4da5f63c1299d75181005 to your computer and use it in GitHub Desktop.
Powershell Retry Logic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Begin { | |
$retryCount = 0 | |
$retryMax = 5 | |
$retryPauseSeconds = 30 | |
} | |
Process { | |
do { | |
$retryCount++ | |
try { | |
#Test your stuff here (for example, try to delete a file in use, then stop the process and watch the script complete after a retry) | |
#Remove-Item -Path "C:\locked_file.exe" -ErrorAction Stop | |
return | |
} catch { | |
Write-Error $_.Exception | |
Write-Output "Retrying in $retryPauseSeconds seconds (Attempt $retryCount of $retryMax)" | |
Start-Sleep -Seconds $retryPauseSeconds | |
} | |
} while ($retryCount -lt $retryMax) | |
throw "Action Unsuccessful after $retryMax attempts" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment