获取子作业的子作业输出
Get child job's child job output
如果你启动了一个作业 (A),而这个作业又启动了另一个作业 (B),是否可以使用类似的东西来获取 (B) 的输出?
(Get-Job -Name A).ChildJobs[0].ChildJobs[0]...
我希望我可以递归地深入到该对象,但奇怪的是 (Get-Job -Name A).ChildJobs[0]
总是有一个空的 ChildJobs 集合。这可能只是我对工作创造方式的误解。
一个解决方法是等到作业 B 完成,获取其输出,将其存储在变量中,然后对该变量执行 Write-Output,以便父脚本可以处理它。它有效,但这意味着我必须等到作业 B 完成,这可能需要 10-40 分钟。
我也许可以(在作业 B 中)将输出立即写入平面文件或 SQLite 数据库,但我希望我可以从脚本的最顶层范围中获取它。
这是一个例子
Get-Job | Remove-Job
$ErrorActionPreference = "stop"
$Level1Job = {
$Level2Job = {
$Level3Job = {
Write-Output "Level 3, start"
Start-Sleep -s 5
Write-Output "Level 3, end"
}
Write-Output "Level 2, start"
# start the third job...
Start-Job -ScriptBlock $Level3Job -Name "level 3"
# wait for the job to complete
while(get-job | where-object { ($_.State -ne "completed") -and ($_.State -ne "failed") }){ Start-Sleep -s 2 }
Start-Sleep -s 5
Write-Output "Level 2, end"
}
Write-Output "Level 1, start"
# start the second job on the remote computer...
Start-Job -ScriptBlock $Level2Job -Name "level 2"
# wait for the job to complete
while(get-job | where-object { ($_.State -ne "completed") -and ($_.State -ne "failed") }){ Start-Sleep -s 2 }
Start-Sleep -s 5
Write-Output "Level 1, end"
}
# start the first job...
Start-Job -ScriptBlock $Level1Job -Name "level 1"
# wait for the job to complete
while(get-job | where-object { ($_.State -ne "completed") -and ($_.State -ne "failed") }){ Start-Sleep -s 2 }
Start-Sleep –s 5
(get-job)[0].ChildJobs[0] | fl *
ChildJobs 用于远程作业。例如,如果您使用 Invoke-Command -ScriptBlock { ... } -AsJob -ComputerName YourRemoteComputer
,您将获得一个远程作业(ChildJob)和一个处理远程作业的本地作业。
您有来自 start-Job
的工作对象。如果你的第一份工作returns这个对象,你可以接收它并读取输出
如果你启动了一个作业 (A),而这个作业又启动了另一个作业 (B),是否可以使用类似的东西来获取 (B) 的输出?
(Get-Job -Name A).ChildJobs[0].ChildJobs[0]...
我希望我可以递归地深入到该对象,但奇怪的是 (Get-Job -Name A).ChildJobs[0]
总是有一个空的 ChildJobs 集合。这可能只是我对工作创造方式的误解。
一个解决方法是等到作业 B 完成,获取其输出,将其存储在变量中,然后对该变量执行 Write-Output,以便父脚本可以处理它。它有效,但这意味着我必须等到作业 B 完成,这可能需要 10-40 分钟。
我也许可以(在作业 B 中)将输出立即写入平面文件或 SQLite 数据库,但我希望我可以从脚本的最顶层范围中获取它。
这是一个例子
Get-Job | Remove-Job
$ErrorActionPreference = "stop"
$Level1Job = {
$Level2Job = {
$Level3Job = {
Write-Output "Level 3, start"
Start-Sleep -s 5
Write-Output "Level 3, end"
}
Write-Output "Level 2, start"
# start the third job...
Start-Job -ScriptBlock $Level3Job -Name "level 3"
# wait for the job to complete
while(get-job | where-object { ($_.State -ne "completed") -and ($_.State -ne "failed") }){ Start-Sleep -s 2 }
Start-Sleep -s 5
Write-Output "Level 2, end"
}
Write-Output "Level 1, start"
# start the second job on the remote computer...
Start-Job -ScriptBlock $Level2Job -Name "level 2"
# wait for the job to complete
while(get-job | where-object { ($_.State -ne "completed") -and ($_.State -ne "failed") }){ Start-Sleep -s 2 }
Start-Sleep -s 5
Write-Output "Level 1, end"
}
# start the first job...
Start-Job -ScriptBlock $Level1Job -Name "level 1"
# wait for the job to complete
while(get-job | where-object { ($_.State -ne "completed") -and ($_.State -ne "failed") }){ Start-Sleep -s 2 }
Start-Sleep –s 5
(get-job)[0].ChildJobs[0] | fl *
ChildJobs 用于远程作业。例如,如果您使用 Invoke-Command -ScriptBlock { ... } -AsJob -ComputerName YourRemoteComputer
,您将获得一个远程作业(ChildJob)和一个处理远程作业的本地作业。
您有来自 start-Job
的工作对象。如果你的第一份工作returns这个对象,你可以接收它并读取输出