When you use the -Command
switch to start PowerShell it runs the supplied command, and then exits the session. Jobs in PowerShell are specific to the session they're started in. You can see this for yourself if you want by starting two instances (sessions) of PowerShell. Create a job in one, e.g. start-Job {sleep 600}
, and do get-Job
, you'll see that a job is Running
. If you type get-Job
into the other PowerShell instance you'll see that there are no jobs.
The start-Job
cmdlet is creating a job, but it doesn't wait for that job to complete, it simply starts it on its merry way. In your case what's happening is that start-Job
is creating the job, you're waiting one second, then listing the contents of the log directory. Then you reach the end of the script and once that happens the session is terminated. This is why only some of your log files are created. The session is being ended because you've reached the end of the script, before the created job has had a chance to finish.
To use jobs in the manner you describe you need for the PowerShell session to remain started for the entirety of the job's lifetime. For that, you'll need the -NoExit
parameter on your PowerShell command that runs the job. You may want to browse around powershell /?
for other switches you might find useful. If you got extra creative with your script that starts the job you could eventually make a hidden, noninteractive session that automatically quit once the job was done. But baby steps.