Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Try/Catch and Retry

Tags:

I have a fairly large powershell scripts with many (20+) functions which perform various actions.

Right now all of the code doesn't really have any error handling or retry functionality. If a particular task/function fails it just fails and continues on.

I would like to improve error handling and implement retries to make it more robust.

I was thinking something similar to this:

$tries = 0 while ($tries -lt 5) {     try{             # Do Something         # No retries necessary        $tries = 5;     } catch {        # Report the error        # Other error handling     }  } 

The problem is that I have many many steps where I would need to do this.

I don't think it make sense to implement the above code 20 times. That seems really superfluous.

I was thinking about writing an "TryCatch" function with a single parameter that contains the actual function I want to call?

I'm not sure that's the right approach either though. Won't I end up with a script that reads something like:

TryCatch "Function1 Parameter1 Parameter2" TryCatch "Function2 Parameter1 Parameter2" TryCatch "Function3 Parameter1 Parameter2" 

Is there a better way to do this?

like image 552
Brad Avatar asked Aug 02 '17 21:08

Brad


1 Answers

If you frequently need code that retries an action a number of times you could wrap your looped try..catch in a function and pass the command in a scriptblock:

function Retry-Command {     [CmdletBinding()]     Param(         [Parameter(Position=0, Mandatory=$true)]         [scriptblock]$ScriptBlock,          [Parameter(Position=1, Mandatory=$false)]         [int]$Maximum = 5,          [Parameter(Position=2, Mandatory=$false)]         [int]$Delay = 100     )      Begin {         $cnt = 0     }      Process {         do {             $cnt++             try {                 $ScriptBlock.Invoke()                 return             } catch {                 Write-Error $_.Exception.InnerException.Message -ErrorAction Continue                 Start-Sleep -Milliseconds $Delay             }         } while ($cnt -lt $Maximum)          # Throw an error after $Maximum unsuccessful invocations. Doesn't need         # a condition, since the function returns upon successful invocation.         throw 'Execution failed.'     } } 

Invoke the function like this (default is 5 retries):

Retry-Command -ScriptBlock {     # do something } 

or like this (if you need a different amount of retries in some cases):

Retry-Command -ScriptBlock {     # do something } -Maximum 10 

The function could be further improved e.g. by making script termination after $Maximum failed attempts configurable with another parameter, so that you can have have actions that will cause the script to stop when they fail, as well as actions whose failures can be ignored.

like image 98
Ansgar Wiechers Avatar answered Sep 18 '22 14:09

Ansgar Wiechers