I am trying to create a job that runs at startup on Windows Server 2012 R2 Core. The job runs a script that connects to a network shares and then mounts a VHD located on that share.
The script itself works and when registering with the -RunNow
parameter the job is fired and runs as expected. Running the job with the Start-Job
Cmdlet also works fine.
However, the job does not fire at startup like specified in its trigger.
What I did to create the job:
$Trigger = New-JobTrigger -AtStartup
$JobOptions = New-ScheduledJobOption -RunElevated -RequireNetwork
Register-ScheduledJob -FilePath C:\Scripts\Test.ps1 -Name Test -Trigger $Trigger -ScheduledJobOption $JobOptions -RunNow
I can clearly see the results of it working after doing this. But when restarting the server the results aren't there, but the job says it was last run not long after the reboot.
Best Answer
When using the New-JobTrigger cmdlet to create a startup trigger it's a good idea to specify a random delay period of 30 seconds or more to avoid conflicts at startup. This will also help ensure a greater chance of success for the job.
You also want to check the status of your job once you've restarted and logged back in with the
Get-Job
cmdlet.