2016-08-08 8 views
0

Ich versuche, Powershell 3. in asynchrone Funktionalität zu testenAusgabe von Skriptblock von Get-Job zurückgeben?

Also dachte ich, dass ich die Uptimes einiger Server aus der Ferne abgefragt werden würde, und schrieb das folgende Skript:

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 
    } 

} 

aber ich don‘ t wirklich scheinen, irgendeine Ausgabe im resultierenden $dasJob Objekt zurück zu erhalten, gab ich den Wert in meinem scriptblock zurück, wohin geht es?

Antwort

0

Sie haben bereits den Ausgang (nicht natürlich Write-Host Ausgang) in der Variablen $results. In PowerShell rufen Sie die Jobausgabe über Receive-Job ab. Die Eigenschaft Output scheint immer leer zu sein (nicht sicher warum).

Verwandte Themen