1

Say I run a PowerShell script like this:

C:\utils\powershell\MDB_BE>powershell -File .\MonitorFileSize.ps1

WARNING: column "Command" does not fit into the display and was removed.

Id              Name            State      HasMoreData     Location
--              ----            -----      -----------     --------
1               Job1            Running    True            localhost

...and that PowerShell script kicks off a Job...

When I open another PowerShell window I notice that the job is not listed when I run Receive-Job 1, so where did the job go, and how can I leave it running, find it again when I exit the Powershell process?

leeand00
  • 4,807
  • 13
  • 64
  • 106

1 Answers1

3

Get-Job only returns job objects from the current session. The job runs as a thread under Powershell.exe, so if you exit Powershell, the job is aborted.

Here is a nice link talking about Powershell jobs:

http://startlearningpowershell.com/?Name=PowerShell%20and%20Background%20Jobs

In the very first section, it says this:

They are not scheduled tasks, and will not run if PowerShell is not currently running.

Edit: Technically, some Powershell jobs spawn entirely new instances of the Powershell process, and some don't.

Edit 2: So to more precisely answer your question: When you kick off Powershell from cmd.exe, cmd.exe spawns an instance of Powershell.exe with cmd.exe as its parent. If the job in that Powershell script then started a PS job that required another Powershell.exe process, another one would be created as a child of the first Powershell.exe. If the PS job in the script is not finishing and your Powershells are closing before the job can finish running, consider working a Wait-Job into your script.

Ryan Ries
  • 55,011
  • 9
  • 138
  • 197
  • Well what if I did spawn a new Powershell process? Would it just show up in the task manager? – leeand00 Aug 13 '12 at 17:43
  • Yes, all processes running on the system should show up in task manager. – Ryan Ries Aug 13 '12 at 17:44
  • So those Job things are more like threads that are kicked off from the main process then they are "jobs" like in Linux that you can start, leave in the background, and bring to the foreground again on any terminal in the system? – leeand00 Aug 15 '12 at 14:05
  • 1
    Correct. Powershell jobs are only for use in the current Powershell session or console, to be used for delegating bits of work to background threads so that the script can move on to do other things while it waits for the job to finish. If you close Powershell, you end any background jobs that were running. – Ryan Ries Aug 15 '12 at 14:28