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.