How can I tell what processes are started by a Start-Job command?

4.3k Views Asked by At

I have (inherited) a PowerShell script that calls other PowerShell scripts by invoking them with a Start-Job cmdlet and the -FilePath parameter. For example, I have a script that does nothing:

Start-Sleep -Seconds 86400

Which I can invoke via a job:

PS> Start-Job -Name "Test Job" -FilePath ".\Wait-AllDay.ps1"

Id Name     PSJobTypeName State   HasMoreData Location  Command                        
-- ----     ------------- -----   ----------- --------  -------                        
2  Test Job BackgroundJob Running True        localhost Start-Sleep...

PS> Get-Job -Id 2

State         : Running
HasMoreData   : True
StatusMessage : 
Location      : localhost
Command       : Start-Sleep -Seconds (60*60*24)
JobStateInfo  : Running
Finished      : System.Threading.ManualResetEvent
InstanceId    : 0c0e2c32-cbc5-4d70-b7cb-28771626cf20
Id            : 2
Name          : Test Job
ChildJobs     : {Job3}
PSBeginTime   : 25/01/2016 15:06:22
PSEndTime     : 
PSJobTypeName : BackgroundJob

Is there any easy & reliable way to find out what process is associated with this job (which I believe will be an additional powershell.exe)? Obviously this is easy in testing with just one job, but on a server I may have many of these running concurrently.

Why do I want to do/know this?

I'm in a new role, working on a server which has multiple scheduled tasks running a variety of scripts. Some of these call other scripts which my predecessor chose to invoke using Start-Job, possibly because they can be long running (multiple hours) and wanted them to work in parallel. Occasionally they seem to get stuck and I'd like to kill them, but don't want to risk stopping something that is still healthy.

Having started down this path, it is probably more plain curiosity as to how to match up jobs and processes, since I'll most likely start rewriting some of these scripts in the near future.

2

There are 2 best solutions below

1
On BEST ANSWER

As far as I can tell, the answer to "can I tell which process matches up with each job" is no.

However, it seems that there are some clues as to which processes are the ones that match up with the job(s).

  1. They have a command line such as:

    "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Version 4.0 -s -NoLogo -NoProfile

  2. They are child processes of either another powershell.exe or powershell_ise.exe process, depending on how they are invoked. If the parent process is called by task manager or other scheduling agent, then the odds are that there will be a script name in the process command line, which may help. In my case this wasn't too much of a help as each scheduled task was spawning ~4 powershell.exe background processes.

  3. If you have details of when each job was started (easy if you can run the Get-Job cmdlet to return the job details), you might be able to match up jobs to processes based on timestamps, but even in simple tests there wasn't an exact match; the job and process were 1-2 seconds apart in starting.

Other than that, I couldn't find any useful way to identify the actual processes linked to each job.

4
On

Using the *-Job cmdlets might be a better approach than fiddling with the actual processes. Use Get-Job to list existing jobs:

PS C:\> Get-Job

Id   Name    PSJobTypeName   State       HasMoreData   Location      Command
--   ----    -------------   -----       -----------   --------      -------
2    Job2    BackgroundJob   Running     False         localhost     Do-Some
4    Job4    BackgroundJob   Completed   True          localhost     Get-Other

The Command property holds the content of the scriptblock the job is/was running.

(Get-Job -Id 2).Command

The State property shows the current state of the job (running, completed, failed, blocked, ...). The HasMoreData property indcates whether the job has output that you can fetch via Receive-Job.

Receive-Job -Id 4

Jobs can be stopped via Stop-Job, and Remove-Job removes terminated jobs from the job list.

See here for further information about background jobs.