想象一下,您创建了返回布尔值的函数(例如Set-SomeConfiguration
)。然后,用
Start-Job -Scriptblock { Set-SomeConfiguration -ComputerName $computer }
有没有办法检索Set-SomeConfiguration
生成的布尔值?
答案 0 :(得分:5)
是的,使用Receive-Job
cmdlet:
$SomeJob = Start-Job { Set-SomeConfiguration -ComputerName $computer }
$Result = $SomeJob | Receive-Job -Wait
-Wait
参数确保Receive-Job
等待作业完成并返回其结果。
(注意:大多数Set-*
cmdlet不会 - 也不应该 - 实际返回任何内容。要实现您描述的内容,您可以返回automatic variable $?
:{{ 1}},或在接收之前检查作业的{Set-SomeConfiguration;$?}
和State
属性。
如果您想要更精细地控制等待时间,请使用Wait-Job
。
在此示例中,我们等待10秒(或直到作业完成):
Error