Powershell timeout service [closed]

牧云@^-^@ 提交于 2019-12-24 18:12:44

问题


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?


回答1:


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)

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

Hope this Helps



来源:https://stackoverflow.com/questions/8566824/powershell-timeout-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!