考虑你有 4 台机器。
在每台远程机器上,您想对前 100 万个素数求和。您可以从本地主机执行此操作:
$servers = @("MachineA","MachineB","MachineC")
Invoke-Command -ComputerName $servers -ScriptBlock {
Sum-FirstMillionPrimes
}
Run Code Online (Sandbox Code Playgroud)
正如所写的那样,在最慢的机器完成之前,不会显示(产生)结果。
为了加快速度,您尝试将其作为一项工作来执行:
$servers = @("MachineA","MachineB","MachineC")
Invoke-Command -ComputerName $servers -ScriptBlock {
Sum-FirstMillionPrimes
} -AsJob
while ($null -ne (Get-Job)) {
$doneChildJob = Get-Job | Wait-Job -Any
$processResult = $doneChildJob | Receive-Job -AutoRemoveJob -Wait
$processResult
}
Run Code Online (Sandbox Code Playgroud)
这仍然有同样的问题,因为根据文档(示例 8):
The command uses the AsJob parameter to run the command as a background
job. This command returns a job object that …Run Code Online (Sandbox Code Playgroud)