超时 Get-WMIObject cmdlet [英] Timeout Get-WMIObject cmdlet
问题描述
我运行了一个执行许多 WMI 查询的脚本 - 但如果服务器没有回答,cmdlet 就会挂起.如果 X 秒过去了,我有什么办法可以使这个(或任何其他与此相关的 cmdlet)超时并退出?
I run a script which performs many WMI-querys - but the cmdlet hangs if the server doesn't answer.. Is there any way I can make this (or any other cmndlet for that matter) timeout and exit if X seconds has passed?
编辑
感谢 mjolinor 的提示,解决方案是将其作为 -asjob 运行并在 while 循环中设置超时.但这已经从作业中运行(从 Start-Job 开始).那么我怎么知道我正在控制正确的工作?
Thanks to a tip from mjolinor the solution is to run this as -asjob and set a timeout in a while loop. But this is run from within a job already (started with Start-Job). So how do I know I am controlling the correct job?
这是我已经开始的工作中的代码:
This is my code from inside my already started job:
Get-WmiObject Win32_Service -ComputerName $server -AsJob
$Complete = Get-date
While (Get-Job -State Running){
If ($(New-TimeSpan $Complete $(Get-Date)).totalseconds -ge 5) {
echo "five seconds has passed, removing"
Get-Job | Remove-Job -Force
}
echo "still running"
Start-Sleep -Seconds 3
}
PS:我从 Start-Jobs 开始的工作已经处理好了..
PS: My jobs started with Start-Jobs are already taken care of..
推荐答案
对于这个问题,我见过的仅有的两个解决方案是:
The only two solutions I've seen for this problem are:
将查询作为后台作业运行并为其设置计时器,然后停止/删除运行时间过长的作业.
Run the queries as background jobs and put a timer on them, then stop/remove the jobs that run too long.
修复您的服务器.
这篇关于超时 Get-WMIObject cmdlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!