开发者

Execute statement with timeout?

开发者 https://www.devze.com 2023-02-19 14:46 出处:网络
Running into a problem using Invoke-WmiMethod in a script, which when run against some computers just hangs. What I\'d like to do is execute the command and if it does not return within some defined p

Running into a problem using Invoke-WmiMethod in a script, which when run against some computers just hangs. What I'd like to do is execute the command and if it does not return within some defined period of time (say 60 to 120 sec's) then move on (logging the b开发者_如何学JAVAailout to a file as well.) The only way I currently know to try to do this is using the -AsJob parameter; however, when I try that, the spawned job immediately fails (even against computers where the Invoke-WmiMethod statement is working without the -AsJob parameter added.) Other WMI-related cmdlets do work against these problem machines, so I don't think it's a auth or DCOM problem.

Anyways, are there other ways of setting a timeout on a statement in a script that I can try?


Hmm, not sure why Invoke-WmiMethod fails but perhaps Invoke-Command will work (worth a quick try I guess) e.g.:

$job = Invoke-Command -cn $computers { Invoke-WmiMethod ... } -AsJob
Wait-Job $job -Timeout 60

This does assume you have enabled remoting on all the remote computers and that they are running PowerShell 2.0. Make sure you run this from an elevated prompt if on Vista/Windows 7 or as admin on XP.

0

精彩评论

暂无评论...
验证码 换一张
取 消