, , ,

Powershell: Simple retry logic

1–2 minutes
12 comments on Powershell: Simple retry logic
Powershell retry loop

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.

Powershell retry loop

# 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)

12 responses

  1. Jeremy Avatar

    I believe the condition in the while loop should be: $Stoploop -eq $false or it will only “retry” when the commands successfully execute.

    1. Thomas Maurer Avatar

      I think you are correct ;) Fixed it

  2. Jay_C Avatar

    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.

  3. Manu Avatar

    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
    }
    }

  4. Craig Tolley Avatar

    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)

  5. Vlad Avatar

    Thank you. Worked great!!!

    1. Thomas Maurer Avatar

      you’re welcome :)

  6. Spencer Avatar

    This was very helpful! Thank you :)

  7. zeb wainwright Avatar
    zeb wainwright

    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!

  8. Steven Durham Avatar
    Steven Durham

    works as hell as fast thanks mate

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.