Joey
Joey

Reputation: 59

Start-Job only runs if I wait for completion

I have a Start-Job -ScriptBlock that will run correctly if I wait for the job to complete. If I don't wait/receive job completion status, the -ScriptBlock does not run. I'm not sure what I'm missing. Likely not understanding fundamental behaviour of PS Background Jobs.

This is running on a Win 2012R2 server. The following is my $PSVersionTable dump:

Name                           Value                                                                 
----                           -----                                                                 
PSVersion                      5.0.10586.117                                                         
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                               
BuildVersion                   10.0.10586.117                                                        
CLRVersion                     4.0.30319.42000                                                       
WSManStackVersion              3.0                                                                   
PSRemotingProtocolVersion      2.3                                                                   
SerializationVersion           1.1.0.1  

I have tried placing tests before and after the -ScriptBlock to catch other errors, but the entire -ScriptBlock doesn't seem to be running at all.

As an example, the following works currently in my setup:

Start-Job -ScriptBlock {
   New-Item -Path 'c:\ivanti-patch-automation\logs\tempfile.txt' -ItemType File
} | Wait-Job

The file is correctly created.

The following does not work. The only change is removing the pipeline command Wait-Job.

Start-Job -ScriptBlock {
   New-Item -Path 'c:\ivanti-patch-automation\logs\tempfile.txt' -ItemType File
}

I expected both to work and am unsure why waiting for the job to complete is influencing whether it does or not.

Upvotes: 1

Views: 969

Answers (3)

Joey
Joey

Reputation: 59

I discovered the answer a few minutes ago. The first comment by PetSerAI alluded a bit to it, though I discovered the answer on my own through research afterwards.

In this case, the parent PS session (a) was initiating the background job (b) via Start-Job. The job (b) was beginning, but before it had a chance to process the very first line within the job (b), the parent PS session (a) had continued on, completing it's run and terminating that session entirely. Since jobs spawned from PS sessions don't out-persist their parents normally, the child background job (b) I started was instantly terminated and never processed a single line within it.

This is also why waiting for the child job seemed to resolve the issue temporarily.

Given that is the issue, creating the background job in any way that outlasts the parent will solve this in my case. I'm going to pursue using Start-Process to instead spawn a new PowerShell process which will outlast the parent process. This should fix the issue for me.

Upvotes: 1

ahmoreish
ahmoreish

Reputation: 320

Add -force:

Start-Job -ScriptBlock {
   New-Item -Path 'c:\ivanti-patch-automation\logs\tempfile.txt' -ItemType File -force
}

Upvotes: 0

Sid
Sid

Reputation: 2676

Hmmm, I am not able to reproduce this. But I am on 5.1. Can you try putting the scriptblock in a variable and passing that to the job. Like

$ScriptBlock = {
   New-Item -Path 'c:\ivanti-patch-automation\logs\tempfile.txt' -ItemType File
}

Start-Job -ScriptBlock $ScriptBlock

See if that creates a job.

Upvotes: 0

Related Questions