Reputation: 560
I'm failing miserably to get any output from jobs - what is wrong with the following code?
$test = {
DIR
}
$mjob = Start-Job -ScriptBlock {$test}
while (Get-Job -State Running){}
Receive-Job -Job $mjob -OutVariable $otest
Write-Host($otest)
Upvotes: 1
Views: 12655
Reputation: 126932
You can use the pipeline to wait for the job to finish and then receive its result. Make sure to remove the braces when you pass a scriptblock to the ScriptBlock parameter, otherwise you're creating a nested scriptblock:
$test = { DIR }
Start-Job -ScriptBlock $test | Wait-Job | Receive-Job
Upvotes: 4
Reputation: 202072
When you use -OutVariable
supply only the name of the variable e.g.:
... -OutVariable otest
unless $otest
happens to contain the name of the variable you want to save the output to.
A few other suggestions. $test
represents a scriptblock so you don't need to put {}
around it. And rather than wait using a while loop, just use Wait-Job
e.g.:
$test = { get-childitem }
$job = Start-Job $test
Wait-Job $job
Receive-Job $job -OutVariable otest
$otest
Upvotes: 7