I'm trying to run Register-ScheduledJob
from a script executing as SYSTEM (from an external deployment tool), but I get an error. This must be initially invoked as SYSTEM due to the way the deployment tool works.
To reproduce this problem (requires powershell.exe running as SYSTEM), I created another scheduled job as follows:
- Run as user account:
SYSTEM
- Action:
- Run:
powershell.exe
- Arguments:
Register-ScheduledJob -name testjob -filepath c:\target.ps1 > C:\testjob.txt 2>&1
- Run:
Note: if you want to reproduce this yourself, you'll also have to create
c:\target.ps1
as an empty file.
This job can then be executed from Task Scheduler UI, and you can see the output in c:\testjob.txt
.
When the script runs Register-ScheduledJob
, it shows the following error:
Register-ScheduledJob : An error occurred while registering scheduled job
definition testjob to the Windows Task Scheduler. The Task Scheduler error is:
(32,4):UserId:.
At line:1 char:1
+ Register-ScheduledJob -name testjob -filepath c:\target.ps1 > C:\testjob.txt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Microsoft.Power...edJobDefini
tion:ScheduledJobDefinition) [Register-ScheduledJob], ScheduledJobExceptio
n
+ FullyQualifiedErrorId : CantRegisterScheduledJobDefinition,Microsoft.Pow
erShell.ScheduledJob.RegisterScheduledJobCommand
As far as I can tell this has something to do with not being able to create a job as the SYSTEM user. I'm not really concerned with what target.ps1
actually runs as (eg it doesn't have to be SYSTEM), so long as it's an account that I don't have to manage a password for.
My script has to be initially invoked as SYSTEM (from a service which is running as SYSTEM), so how can I use it to create a scheduled job?