PowerShell启动服务超时 [英] PowerShell Start-Service Timeout

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

问题描述

我一直在为应用程序的Start-Services功能开发超时功能,偶然发现一些挫折,其中超时无法正常工作或无法正确显示.

I have been working on a timeout feature for the Start-Services feature of my application and have stumbled upon some setbacks in which the timeout doesn't work properly or fails to display properly.

我的两个常量设置如下:

My two constants are set as such:

$timeout = New-TimeSpan -Seconds $SecondsToWait
$elapsedTime = [System.Diagnostics.Stopwatch]::StartNew()

我的其余代码已多次更改,以尝试实现我的目标.我尝试了以下所有方法,但几乎没有成功.每次尝试都会有一个简短的描述:

The rest of my code has changed multiple time to try and achieve my goal. I have tried all of the following with little or no success. Each attempt will have a brief description:

在这种情况下,我尝试创建一个新作业,将操作放入Do While循环内,如果经过时间=超时时间或作业状态为正在运行",则退出所说的循环.不幸的是,这在while循环中中断.

In this instance, I attempted to create a new job, put the action inside of a Do While loop and Exit said loop if Elapsed Time = Timeout time or Job State is 'Running'. Unfortunately, this breaks during the while loop.

$j = Start-Job -ScriptBlock { Start-Service $ServiceName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue }

Do {
    If ($j.State -eq "Running") { break }
    $j | Receive-Job
} While ($elapsedTime -le $timeout)

我尝试了使用另一种格式的Do While循环,但是在它运行到无法自动启动的服务时,它在Start-Service行遇到无限循环时仍然无法正常工作.

I attempted the Do While loop in a different format, but still had issues getting it to work as it hits an infinite loop at the Start-Service line when it runs into a service it cannot start automatically.

Do {
    $counter++
    Start-Sleep 1
    Start-Service $ServiceName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue

    # this line is just a custom function that gets the status of the service and checks a number of times based on the 'SecondsToWait' Parameter. 
    Wait-ServiceState -ServiceName $ServiceName -ServiceState "Running" -SecondsToWait 1
} While ($elapsedTime -le $timeout)

由于与上述实例类似的原因,这在Start-Service Cmdlet上也中断了

This too breaks at the Start-Service Cmdlet for a similar reason as the above instance

Do {
    $counter++
    Start-Service $ServiceName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
} While ($elapsedTime -eq $timeout)

我还尝试使用Write-Progress代替它作为更具动态性的变量...但是在初始化Start-Services参数方面并没有太大作用.

I also tried using Write-Progress instead as a more dymanic variant... but that didn't do much in terms of initializing the Start-Services argument.

我也尝试通读了几篇文章,希望找到其他方法来实现此目的,但即使是那些变体也失败了……而且我非常喜欢Write-Progress的工作方式……我无法理解如何从屏幕上清除它(而不清除整个cmd屏幕)或如何控制它在cmd窗口中的显示位置.无论如何,您可以在下面找到这些替代资源:

I have tried reading through several posts as well in hopes of finding alternative ways to do this, but even those variations failed... And as much as I liked the way Write-Progress worked... I couldn't understand how to clear it from the screen (without clearing the entire cmd screen) or how to control where it appears on the cmd window. Anyways, you can find those alternative resourses below:

Powershell超时服务

https ://docs.microsoft.com/zh-CN/powershell/module/microsoft.powershell.core/start-job?view = powershell-5.1

Powershell启动过程,请等待超时,杀死并获取退出代码

https ://docs.microsoft.com/zh-CN/powershell/module/microsoft.powershell.utility/write-progress?view = powershell-5.1

有人知道如何解决我的代码中的问题,这些问题不仅可以使Start-Process超时,而且可以正确显示经过的时间?

Does anyone know how to resolve the issues with my code that would help me not only timeout the Start-Process, but to display the elapsed time properly?

预先感谢

推荐答案

您创建了一个名为$elapsedTime

$elapsedTime = [System.Diagnostics.Stopwatch]::StartNew()

这有点令人困惑,因为您选择的名称看起来并不真实. 因此,当您想与超时进行比较时,$ elapsedTime不是表示为时间跨度的经过时间,因为您可能会很快想到读取变量名,它是您的Stopwatch对象.

That is kind of confusing because the name you chose is not what it seems. Therefore, when you want to compare against your timeout, $elapsedTime is not the elapsed time represented as a timespan, as you might think quickly reading variable name, it is your Stopwatch object.

因此,您的

 While ($elapsedTime -le $timeout)

实际上应该是

While ($elapsedTime.Elapsed -le $timeout)

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

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