Skip to content

Instantly share code, notes, and snippets.

@jcefoli
Created December 6, 2021 19:44
Show Gist options
  • Save jcefoli/42a01a4681d4da5f63c1299d75181005 to your computer and use it in GitHub Desktop.
Save jcefoli/42a01a4681d4da5f63c1299d75181005 to your computer and use it in GitHub Desktop.
Powershell Retry Logic
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