如何超时PowerShell函数调用 [英] How to timeout PowerShell function call
本文介绍了如何超时PowerShell函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个小 Powershell 函数,它对远程服务器执行 Get-EventLog.在某些服务器上,这似乎只是挂起并且永远不会超时.我可以超时 powershell 函数调用吗?我了解如何针对 不同进程执行此操作,但我想要为 power shell 函数执行此操作.
I wrote a little powershell function that executes Get-EventLog against remote servers. On some servers this seems to just hang and never times out. Can I timeout a powershell function call? I see how to do this against a different process, but i want to do this for a power shell function.
谢谢
#######################
function Get-Alert4
{
param($computer)
$ret = Get-EventLog application -after (get-date).addHours(-2) -computer $computer | select-string -inputobject{$_.message} -pattern "Some Error String" | select-object List
return $ret
} #
推荐答案
您可以通过使用后台作业来实现超时,如下所示:
You can implement timeouts by using a background job like so:
function Get-Alert4($computer, $timeout = 30)
{
$time = (Get-Date).AddHours(-2)
$job = Start-Job { param($c) Get-EventLog Application -CN $c -After $time |
Select-String "Some err string" -inputobject{$_.message} |
Select-Object List } -ArgumentList $computer
Wait-Job $job -Timeout $timeout
Stop-Job $job
Receive-Job $job
Remove-Job $job
}
这篇关于如何超时PowerShell函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文