Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

powershell - 经过一定时间后是否中断PS命令?(Breaking out of a PS command after a certain amount of time?)

I'm looking for an example that can break out of the current command if it runs for longer than say, 1 minutes, which would allow me to re-run it.

(我正在寻找一个示例,如果该命令运行超过一分钟(例如1分钟),则该示例可能会中断运行,这将允许我重新运行该命令。)

Here is my script :

(这是我的脚本:)

$ServiceName = 'service'
$arrService = Get-Service -Name $ServiceName

while ($arrService.Status -ne 'Running')
{

    Start-Service $ServiceName
    write-host $arrService.status
    write-host 'Service starting'
    Start-Sleep -seconds 3
    $arrService.Refresh()
    if ($arrService.Status -eq 'Running')
    {
        Write-Host 'Service is now Running'
    }

}
  ask by Arbelac translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use Start-Job Start-Job cmdlet to run the process.

(使用Start-Job Start-Job cmdlet运行该过程。)

Then define Wait-job Wait-Job cmdlet with a -timeout .

(然后使用-timeout定义Wait-job Wait-Job cmdlet 。)

The -timeout defines the time for the wait to end and the command prompt will return.

(-timeout定义等待结束的时间,命令提示符将返回。)

A point to remember: -timeout don't display any error messages.

(需要记住的一点是: -timeout不显示任何错误消息。)

Keep that in mind while doing error handling.

(在进行错误处理时,请记住这一点。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...