I am working on some larger PowerShell scripts right now and so I needed to create a simple retry logic for sending web request to a server. This here gives you a simple retry logic for PowerShell.
# Comment $Stoploop = $false [int]$Retrycount = "0" do { try { Scripts Commands here Write-Host "Job completed" $Stoploop = $true } catch { if ($Retrycount -gt 3){ Write-Host "Could not send Information after 3 retrys." $Stoploop = $true } else { Write-Host "Could not send Information retrying in 30 seconds..." Start-Sleep -Seconds 30 $Retrycount = $Retrycount + 1 } } } While ($Stoploop -eq $false)
I believe the condition in the while loop should be: $Stoploop -eq $false or it will only “retry” when the commands successfully execute.
I think you are correct ;) Fixed it
Thanks, this helped me out!
FYI… if you use this with a “copy-item” or “move -item” as your script command, you will run into a non-terminating error condition, as I did. in this case add an:
$ErrorActionPreference = “Stop”
above the command to resolve this.
Hi, the following logic nails it for me:
function Get-ContentRetry {
param (
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string]$path,
[int]$retries = 5,
[int]$wait = 1
)
[int]$Retrycount = “0”
$return = $null
$Stoploop = $false
do {
try {
$return = Get-Content -path $path -erroraction stop
$Stoploop = $true
}
catch {
if ($Retrycount -gt $retries){
$Stoploop = $true
throw $_.Exception
}
else {
if($_.Exception.ErrorRecord.CategoryInfo.Category -eq “ObjectNotFound”){
return $null
}
Start-Sleep -Seconds $wait
$Retrycount = $Retrycount + 1
}
}
}
while ($Stoploop -eq $false)
return $return
}
function Out-FileRetry {
param (
[Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
$Content,
[Alias(“path”)]
[string]$filepath,
[int]$retries = 5,
[int]$wait = 1,
[switch]$noforce,
[switch]$force # only for validation
)
begin {
[int]$Retrycount = “0”
$return = $null
$Stoploop = $false
$cobj = @()
}
process {
$cobj += $_
}
end {
do {
try {
if($noforce){
$return = $cobj | out-file -filepath $filepath -erroraction stop
} else {
$return = $cobj | out-file -filepath $filepath -force -erroraction stop
}
$Stoploop = $true
}
catch {
if ($Retrycount -gt $retries){
$Stoploop = $true
throw $_.Exception
}
else {
Start-Sleep -Seconds $wait
$Retrycount = $Retrycount + 1
}
}
}
while ($Stoploop -eq $false)
return $return
}
}
Hi,
Your code will actually retry 4 times. On line 13 the comparator should be “-ge” and the increment line currently at line 20 should be placed between the Do and Try lines – i.e
Do {
$RetryCount = $RetryCount + 1
Try {
#Action Code
Write-Host “Job completed”
$StopLoop = $true
} Catch {
Write-Verbose $_
If ($RetryCount -ge $Retries) {
Write-Host “Action failed after $Retries retries.”
$StopLoop = $true
} Else {
Write-Verbose “Retry in $RetryWaitSecs.”
Start-Sleep -Seconds $RetryWaitSecs
}
}
}
While ($StopLoop -eq $false)
If you are doing an operation like Invoke-WebRequest , retry is built in.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?WT.mc_id=thomasmaurer-blog-thmaure&view=powershell-7
Thank you. Worked great!!!
you’re welcome :)
This was very helpful! Thank you :)
This is amazing. I couldn’t wrap my mind around the logic to make something like this work and our scripts for getting data out of Azure kept timing out. This completely solved that issue. Thank you so much!
works as hell as fast thanks mate
you’re welcome!