Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用 GCI cmdlet 获取有关许多远程计算机的信息,其中一些计算机有时会关闭或无响应。有没有办法可以为 get-childitem 指定超时属性,以便脚本每次遇到无响应的计算机时不会挂起 15-20 秒?
谢谢,
托梅克
做你的工作,然后使用 wait-job (它有一个超时参数)等待它:
$job = Start-Job {Sleep -seconds 60} $res = Wait-Job $job -timeout 5 if(-not $res) { write-Host "Timeout"}
或者将您的 gci 包装在测试连接中
if (test-connection $server -quiet){ gci } else {"连接到 $server 失败"}