# runnin a job within a job, and getting back that data

**URL:** <https://forums.powershell.org/t/runnin-a-job-within-a-job-and-getting-back-that-data/46>\
**Category:** PowerShell Help\
**Created:** [May 13, 2013, 3:56am UTC](https://forums.powershell.org/t/runnin-a-job-within-a-job-and-getting-back-that-data/46 "2013-05-13T03:56:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iancaseydouglas](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/iancaseydouglas/32/506_2.png) [@iancaseydouglas](https://forums.powershell.org/u/iancaseydouglas)\
**Post date:** [May 13, 2013, 3:56am UTC](https://forums.powershell.org/t/runnin-a-job-within-a-job-and-getting-back-that-data/46/1 "2013-05-13T03:56:37Z")

</div>

I am using remoting to execute a scriptblock on a group of servers, and using the â€˜-asjobâ€™ parameter to run it in the background. For example:

&nbsp;

1\> Â $job = Invoke-Command -ComputerName $ComputerName -scriptblock $ScriptBlock -jobname $JobName -throttlelimit $ThrottleLimit -AsJob

&nbsp;

Once the job has completed, I write the results and the errors into variables, eg,

&nbsp;

2\> Â $jobresult = Get-Job -Name $jobName |Â Wait-Job | Receive-Job -ErrorVariable $ErrorVariableName -ErrorAction SilentlyContinue

&nbsp;

This works fine, Â but I have to wait around for the job to finish in order to issue the above command, when it would be much better for another wrapper job to run that waits for the childjobs to complete, then writes the results into the variable $jobresult. For example, replace (2) with something like:

&nbsp;

3\> Â Start-Job -Name JobExec -ScriptBlock { $jobresult = $job | Wait-Job | Receive-Job -ErrorVariable $errorVariableName -ErrorAction SilentlyContinue}

&nbsp;

If I issue 1 & 3 above, everything executes, but thereâ€™s no data written into $jobresultÂ ( at least not that I can get out) or $errorVariableName . Is there something Iâ€™m missing with variable scoping here? Something else I’m missing altogether? Iâ€™ve tried forcing $jobresult to have global scope, with no luck. Please advise!

I want to be able to run 1 and 3 together in the background, and when they are done, both $jobresult and $errorVariable will be populated with data, and in the meantime I’m free to use the console, without having to wait to issue the receive-job

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [May 13, 2013, 7:37am UTC](https://forums.powershell.org/t/runnin-a-job-within-a-job-and-getting-back-that-data/46/2 "2013-05-13T07:37:59Z")

</div>

Been watching Inception, have we?

Do this in a script. In between your 1 & 2 commands, put Wait-Job. Have it wait until all jobs complete. Then, just run the script as a job. I’m curious to see if that makes a difference.

I need to spend a sec further unwinding the syntax and its implications, but the nesty-job is the oddness.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:57pm UTC](https://forums.powershell.org/t/runnin-a-job-within-a-job-and-getting-back-that-data/46/3 "2024-05-16T20:57:54Z")

</div>


