powershell - PowerShell 的测试路径超时
问题描述
我正在尝试定期检查我们域中数百台计算机上的文本文件中是否存在特定字符串。
foreach ($computer in $computers) {
$hostname = $computer.DNSHostName
if (Test-Connection $hostname -Count 2 -Quiet) {
$FilePath = "\\" + $hostname + "c$\SomeDirectory\SomeFile.txt"
if (Test-Path -Path $FilePath) {
# Check for string
}
}
}
在大多数情况下Test-Connection
,然后的模式Test-Path
是有效且快速的。但是,有些计算机可以 ping 成功,但Test-Path
需要大约 60 秒才能解析为FALSE
. 我不确定为什么,但这可能是域信任问题。
对于这样的情况,我希望有一个超时Test-Path
,默认为FALSE
如果它需要超过 2 秒。
不幸的是,相关线程中的解决方案(How can I wrap this Powershell cmdlet into a timeout function?)不适用于我的情况。建议的 do-while 循环被挂在代码块中。
我一直在尝试乔布斯,但似乎即使这样也不会强制退出Test-Path
命令:
Start-Job -ScriptBlock {param($Path) Test-Path $Path} -ArgumentList $Path | Wait-Job -Timeout 2 | Remove-Job -Force
作业继续在后台挂起。这是我可以达到上述要求的最干净的方法吗?有没有更好的方法来超时 Test-Path 以便脚本除了产生异步活动之外不会挂起?非常感谢。
解决方案
将您的代码包装在一个[powershell]
对象中并调用BeginInvoke()
以异步执行它,然后使用关联的 WaitHandle 等待它仅在设定的时间内完成。
$sleepDuration = Get-Random 2,3
$ps = [powershell]::Create().AddScript("Start-Sleep -Seconds $sleepDuration; 'Done!'")
# execute it asynchronously
$handle = $ps.BeginInvoke()
# Wait 2500 milliseconds for it to finish
if(-not $handle.AsyncWaitHandle.WaitOne(2500)){
throw "timed out"
return
}
# WaitOne() returned $true, let's fetch the result
$result = $ps.EndInvoke($handle)
return $result
在上面的示例中,我们随机休眠 2 秒或 3 秒,但设置了 2 秒半的超时时间 - 尝试运行几次以查看效果 :)
推荐阅读
- visual-studio - 已安装 Outlook VSTO 机器范围,但显示 formregion 需要 HKCU 注册表项
- xamarin - 使用 WhatsApp 通过 Xamarin 发送图片
- opencv - 如何拉直图像中的不规则线条
- jsf - 在 Primefaces 10 中,没有为 textEditor 组件显示客户端验证要求消息
- python-3.x - 在mongoengine Model python中将默认值设置为Null
- c# - 将 ASP.NET Core 托管的 Blazor WASM 5.0.9 部署到 IIS 10
- angular - 一段时间不活动后,为什么会出现 401 错误?
- excel - 给定任意网络链接列表,如何计算背板吞吐量?
- javascript - 将道具传递给孩子反应时出现未定义的错误
- android - 片段中的 registerForActivityResult