asynchronous - Returning output from scriptblock from Get-Job? -


i'm trying test out asynchronous functionality in powershell 3.

so figured query uptimes of servers remotely, , wrote following script:

function getserveruptimes() {     # obtain credentials logging      # remote machines...     $cred = get-credential      $compshash = @{        "server1.domain.com" = $null;        "server2.domain.com" = $null;     }      # define async block run...no blocking in 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 async process create cimsession          # on each of these machines...         start-job -scriptblock $cmd -argumentlist $comp, $cred -name "get$($comp)"       }      $results = get-job | wait-job | receive-job      # retrieve job, can @ output     foreach($comp in $comphash.keys) {         $dasjob = get-job -name "get$($comp)"         write-host $dasjob.output     }  } 

however, don't seem output in resulting $dasjob object, returned value in scriptblock, going?

you have output (not including write-host output, of course) in variable $results. in powershell retrieve job output via receive-job. property output seems empty (not sure why).


Comments