Receive-Job with -NoRecurse returns NULL - by Pattify

Status : 


Sign in
to vote
ID 771705 Comments
Status Active Workarounds
Type Bug Repros 0
Opened 11/20/2012 3:00:33 AM
Access Restriction Public


When using Receive-Job cmdlet with the -NoRecurse option, the resulting output is an empty string

Tested in PowerShell 2.0 on Windows 7 and PowerShell 3.0 on Windows 2008
Sign in to post a comment.
Posted by Pattify on 11/21/2012 at 1:56 PM
Thank you Roman, this now makes sense as to why it behaves this way.

What I don't understand is why it's designed this way. Why does the job "FooBar" need to fork off another job to execute the script block, which seems to render the -NoRecurse option useless in the process. Is there another way of starting jobs and returning just the status of the code executed in -ScriptBlock other than what i'm currently using?

$job = Start-Job -Name "FooBar" -ScriptBlock { return 2 }
Receive-Job -Job $job |Select -Last 1

Am I just not writing it the way Microsoft intended me to?
Posted by Roman Kuzmin on 11/21/2012 at 7:09 AM
If you run

    $job = Start-Job -Name "FooBar" -ScriptBlock { return 2 }
    Get-Job -IncludeChildJob

you will see 2 jobs, a parent and its child. It's the child that gets actual output, such is the design. By -NoRecurse you ask for the parent output with is NULL and you get NULL. This is my understanding, all looks correct.
Posted by Pattify on 11/21/2012 at 4:03 AM
Hi Roman,

Ok maybe I made a statement that was too broad and there are some scenarios that i've not factored in.

If you run these two commands though:
$job = Start-Job -Name "FooBar" -ScriptBlock { return 2 }
Receive-Job -Job $job -NoRecurse

What do you receive? And is the output different to:
$job = Start-Job -Name "FooBar" -ScriptBlock { return 2 }
Receive-Job -Job $job

Because according from my interpretation of the -NoRecurse option it should affect the output in this scenario, only when multiple child jobs are spawned.

I'll update the title
Posted by Roman Kuzmin on 11/21/2012 at 3:43 AM
Hi Pattify,

My comment just says that the statement “Receive-Job with -NoRecurse always returns NULL” is not true. I have tried it for some jobs and got not NULL output.

Sorry but I cannot answer your questions. I did not work with parent and child jobs separately yet.
Posted by Pattify on 11/20/2012 at 2:36 PM
Hi Roman,

Receive-job, when used without -NoRecurse, returns the status of $job as expected. My understanding is that NoRecurse tells Receive-Job not to return the status of child jobs where $job is the parent job (not the script we're running). Why I wanted to use this is because I had a child job that would fork off other jobs and Receive-Job was returning all their statuses, when I only wanted the output from $job.

To make NoRecurse return the status of the parent script and not $job would make no sense.

In this scenario, because $job does not fork any further child jobs the resulting output with or without NoRecurse should be the same. Can you confirm this for me?

Posted by Roman Kuzmin on 11/20/2012 at 4:30 AM
Is it possible that you actually get exactly what you ask for, i.e. for a *parent* job you get no output because its output actually belongs to a child job? Try to run Get-Job with -IncludeChildJob and you will see perhaps that there are child jobs. If you run Receive-Job with -NoRecurse for such a child job then you will get expected output. Is this the case? At least, this is what I see (PS v3, Windows 7).