Powershell 超时服务 [英] Powershell timeout service

查看:63
本文介绍了Powershell 超时服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Powershell 中启动服务.如果服务在指定的时限内没有启动,启动的尝试应该被终止,否则它应该照常进行.如何合并超时?

I am trying to start a service in Powershell. If the service does not start within a specified time limit, the attempt to start should be killed, otherwise it should carry on as usual. How do I incorporate a timeout?

推荐答案

使用后台作业实现超时是一件非常简单的事情:

Implementing a timeout is a really simple thing to do using background jobs:

$timeout = [Timespan]"0:0:15"
$start = Get-Date
$j = Start-Job -ScriptBLock { Import-Module MyModule; Invoke-MyCommand}
do {
    if ($j.JobState -ne 'Running') { break} 
    $j | Receive-Job
} while (((Get-Date) - $start) -le $timeout)

此代码块将在 15 秒后超时,但它应该可以帮助您入门.基本上:- 跟踪时间- 开始工作- 等待它不再运行(它可能已经失败,所以不要只是等待完成)- 在等待时接收结果- 等待时间过长的超时

This block of code will timeout after 15 seconds, but it should get you started. Basically: - Track the time - Start a job - Wait for it to be no longer running (it could have failed, so don't just wait for completed) - Receive Results while you wait - Timeout if you've been waiting too long

希望对你有帮助

这篇关于Powershell 超时服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆