从Start-Job -ScriptBlock启动后返回值的函数

时间:2015-10-30 17:11:17

标签: powershell jobs

想象一下,您创建了返回布尔值的函数(例如Set-SomeConfiguration)。然后,用

调用该函数
Start-Job -Scriptblock { Set-SomeConfiguration -ComputerName $computer }

有没有办法检索Set-SomeConfiguration生成的布尔值?

1 个答案:

答案 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
相关问题