2016-08-08 5 views
0

私はだから私は、私はリモートで一部のサーバーの稼働時間を問い合わせると考えましたし、次のスクリプトを書いたのPowerShell 3にGet-Jobからscriptblockの出力を戻していますか?

を非同期機能をテストしようとしています:

function getServerUptimes() { 
    # Obtain credentials for logging into 
    # the remote machines... 
    $cred = Get-Credential 

    $compsHash = @{ 
     "server1.domain.com" = $null; 
     "server2.domain.com" = $null; 
    } 

    # Define an async block to run...no blocking in this script... 
    $cmd = { 
     param($computer, $dasCred) 

     Write-Host "which: $($computer)" 
     $session = new-cimsession $computer -Credential $dasCred 

     return (get-CimInstance win32_operatingsystem -CimSession $session | Select PScomputername, LastBootuptime); 
    } 

    ForEach($comp in $compsHash.Keys) { 
     Write-Host "${comp}" 

     # Kick off an async process to create a cimSession 
     # on each of these machines... 
     Start-Job -ScriptBlock $cmd -ArgumentList $comp, $cred -Name "Get$($comp)" 
    } 

    $results = Get-Job | Wait-Job | Receive-Job 

    # Retrieve the job, so we can look at the output 
    ForEach($comp in $compHash.Keys) { 
     $dasJob = Get-Job -Name "Get$($comp)" 
     Write-Host $dasJob.output 
    } 

} 

しかし、私は「ドン結果の$dasJobオブジェクトに出力が戻ってきたようですが、スクリプトブロック内の値を返しました。どこに行くのですか?

答えて

0

変数$resultsには既に出力(Write-Host出力は含まれていません)があります。 PowerShellでは、ジョブ出力をReceive-Jobで取得します。プロパティOutputは常に空であるようです(理由は不明)。

関連する問題