function RetryCommand { [CmdletBinding()] param ( [parameter(Mandatory, ValueFromPipeline)] [ValidateNotNullOrEmpty()] [scriptblock] $ScriptBlock, [ValidateNotNullOrEmpty()] [string] $Context, [int] $RetryCount = 10, [int] $TimeoutInSeconds = 30, [System.Management.Automation.SwitchParameter] $OnFinalExceptionContinue ) process { $attempts = 1 $lastRetryException = $null do { try { Invoke-Command -ScriptBlock $ScriptBlock -OutVariable Result break; } catch { if ($attempts -le $retryCount) { Write-Host "[$attempts/$retryCount] Failed to execute command '$context'. Retrying in $timeoutInSeconds seconds..." -ForegroundColor Yellow Start-Sleep -Seconds $timeoutInSeconds $attempts++ } if ($attempts -eq $retryCount) { Write-Host "Failed to execute command '$context'. Total retry attempts: $retryCount" -ForegroundColor Red $e = $_.Exception $msg = $e.Message while ($e.InnerException) { $ e = $e.InnerException $msg += "`n" + $e.Message } $lastRetryException = $msg Write-Host $_ -ForegroundColor Red } } } while ($attempts -le $retryCount) if ($attempts -ge $retryCount -and $OnFinalExceptionContinue -eq $false) { Write-Host "[Error Message] for '$context'. The message was: $($lastRetryException) `n" -ForegroundColor Red throw $lastRetryException } } } function CheckErrors { [CmdletBinding()] param ( [System.Collections.ArrayList]$ErrorToProcess ) process { $message = $null if ($errorToProcess.Count -ge 1) { foreach ($error in $errorToProcess) { if ($error.Exception.Message) { $message = $error.Exception.Message break } } $errorToProcess.Clear() if ($message) { throw($message) } } } } Export-ModuleMember -Function RetryCommand Export-ModuleMember -Function CheckErrors # SIG # Begin signature block # MIIYJAYJKoZIhvcNAQcCoIIYFTCCGBECAQExDzANBglghkgBZQMEAgEFADB5Bgor # BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG # KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCAVqftZp4J2896T # RoCsX0JFUKfJEGMfQIpXllpET3WCkaCCFGUwggWiMIIEiqADAgECAhB4AxhCRXCK # Qc9vAbjutKl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