Powershell: get output from Receive-Job

If the job uses Write-Host to produce output, Receive-Job returns $null, but the results get written to the host. However, if the job uses Write-Output to produce output in lieu of Write-Host, Receive-Job returns a string array [string[]] of the job output.

To demonstrate, enter this innocuous code at the PowerShell prompt:

$job = Start-Job -ScriptBlock {
    [int] $counter = 0
    while ($counter -lt 10) {
        Write-Output "Counter = $counter."
        Start-Sleep -Seconds 5
        $counter++
    }
}

Wait about 20-30 seconds for the job to produce some output, then enter this code:

$result = Receive-Job -Job $job
$result.Count
$result
$result | Get-Member

The $result object contains the strings produced by the job.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)