shell 执行失败重试_PowerShell尝试/捕获并重试

bd96500e110b49cbb3cd949968f18be7.png

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值