Public/System/Invoke-Retry.ps1
function Invoke-Retry { [CmdletBinding()] Param( [Parameter(Position=0, Mandatory=$true)] [scriptblock]$ScriptBlock, [Parameter(Position=1, Mandatory=$false)] [int]$Maximum = 5 ) Begin { $cnt = 0 } Process { do { $cnt++ try { $ScriptBlock.Invoke() return } catch { [System.Threading.Thread]::Sleep([System.TimeSpan]::FromSeconds(1)) Write-Error $_.Exception.InnerException.Message -ErrorAction Continue } } while ($cnt -lt $Maximum) throw 'Execution failed.' } } |