1

I have several PHP scripts, which execute a Powershell Script to obtain performance counters of my server infrastructure. As I have multiple scripts, I wanted to execute them all at the same time, from just one location.

I have created the following Start-Job script, which when run in the ISE, works perfectly fine.

Start-Job Script

When I run the ps1 script itself, using the below command, I get a list of the jobs that have started, but nothing actually happens. It just doesn't do anything, but also gives me zero errors.

Executing PS1 file

So, what am I missing?

DARKOCEAN
  • 109
  • 3
  • 14

1 Answers1

4

Per the comments, when the calling powershell session terminates it is terminating all of the background jobs it started.

To make it wait for the jobs to complete before terminating add Get-Job | Wait-Job at the end of your script.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • 1
    Wow, finally, the reason for my problem, after a full day of debugging - +1. But where on earth is it documented that the background jobs are terminated when the calling session terminates? I have read the whole documentation page about `Start-Job` at Microsoft, but could not find anything about it. – Binarus Dec 08 '20 at 18:50
  • This behavior is utterly brain-dead: why else would I start background jobs if I wanted to wait until they complete? This has cost me (like the OP) over a day refactoring the script to support running in the background, then validating it worked, then puzzling and debugging only to find this post and realize it won't work when I take out the Wait-Job I was using during debugging... – sprockets Dec 23 '21 at 18:17