0

ジョブ (A) を開始し、このジョブが別のジョブ (B) を開始した場合、そのようなものを使用して (B) の出力をフェッチすることは可能ですか?

(Get-Job -Name A).ChildJobs[0].ChildJobs[0]...

オブジェクトに再帰的にドリルダウンできることを望んでいましたが、奇妙なことに、(Get-Job -Name A).ChildJobs[0]常に空の ChildJobs コレクションがあります。これは、雇用の創出方法に関する私の誤解に過ぎないかもしれません。

これに対する 1 つの回避策は、ジョブ 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 *
4

1 に答える 1

0

ChildJobs は、リモート ジョブに使用されます。たとえばInvoke-Command -ScriptBlock { ... } -AsJob -ComputerName YourRemoteComputer、リモート ジョブ (ChildJob) と、リモート ジョブを処理するローカル ジョブを取得します。

からの Job-Object がありstart-Jobます。最初のジョブがこのオブジェクトを返す場合、それを受け取って出力を読み取ることができます

于 2015-09-01T17:17:36.790 に答える