How can a PowerShell job wait on a Job from the parent session?

104 Views Asked by At

In order to do some work when another job completes without blocking the current session, I want to create a job that waits on another, however when create the second job, it cannot detect the first job.

$Job = Start-Job -ScriptBlock { Start-Sleep -Seconds 30 }
Start-Job -ScriptBlock { param([int]$JobId)  Wait-Job -Id $JobId; [Console]::Beep() } -ArgumentList $Job.Id

Results in

The command cannot find a job with the job ID 17. Verify the value of the Id parameter and then try the command again.

It seems when you start a job, it gets its own session with its own job ids. A job can start sub-jobs and can wait on those jobs. Is there a way for a job to wait on a job in the parent session (a sibling job)?

I can't find anything about this in the about_Jobs or about_Job_Details documentation.

0

There are 0 best solutions below