github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cloudconfig/windowsuserdatafiles/retry.ps1 (about)

     1  
     2  $ErrorActionPreference = "Stop"
     3  
     4  function ExecRetry($command, $retryInterval = 15)
     5  {
     6  	$currErrorActionPreference = $ErrorActionPreference
     7  	$ErrorActionPreference = "Continue"
     8  
     9  	while ($true)
    10  	{
    11  		try
    12  		{
    13  			& $command
    14  			break
    15  		}
    16  		catch [System.Exception]
    17  		{
    18  			Write-Error $_.Exception
    19  			Start-Sleep $retryInterval
    20  		}
    21  	}
    22  
    23  	$ErrorActionPreference = $currErrorActionPreference
    24  }
    25  
    26  # TryExecAll attempts all of the commands in the supplied array until
    27  # one can be executed without throwing an exception. If none of the
    28  # commands succeeds, an exception will be raised.
    29  function TryExecAll($commands)
    30  {
    31  	$currErrorActionPreference = $ErrorActionPreference
    32  	$ErrorActionPreference = "Continue"
    33  
    34  	foreach ($command in $commands)
    35  	{
    36  		try
    37  		{
    38  			& $command
    39  			$ErrorActionPreference = $currErrorActionPreference
    40  			return
    41  		}
    42  		catch [System.Exception]
    43  		{
    44  			Write-Error $_.Exception
    45  		}
    46  	}
    47  
    48  	$ErrorActionPreference = $currErrorActionPreference
    49  	throw "All commands failed"
    50  }