I get the following error in the Scheduled Task history when I try to run a task:
Task Scheduler successfully completed task "\Restart Tomcat" ,
instance "{264b4620-5f3b-6c5f-a6cb-1625a7fa57de}" ,
action "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.EXE"
with return code 1.
The scheduled task is configured as follows:
- Name: Restart Tomcat
- User: DOMAIN\tomcat.restarter
- Triggers: Daily 2AM Enabled
- Actions: Start a Program:
- Program/script:
powershell
- Arguments:
-Command "Restart-Service Tomcat6"
- Program/script:
When I launch a Command Prompt as DOMAIN\tomcat.restarter with:
runas /user:DOMAIN\tomcat.restart cmd
And run:
powershell -Command "Restart-Service Tomcat6"
Then echo %errorlevel%
prints 0
and Tomcat gets restarted. This shows that the SDDL on the Tomcat6 service is sufficient for the purpose and that DOMAIN\tomcat.restarter can restart it.
If I change the scheduled task arguments to -Command "'hello world'" > '%TEMP%\Temp.log'
I get return code 0
in the Task History and hello world
shows up in C:\Users\tomcat.restarter\AppData\Local\Temp\Temp.log
. This shows that the Log on as a batch job User Right is effective for DOMAIN\tomcat.restarter, that it can run Powershell and that it can write files.
UPDATE: Further investigation
I created a restart.bat in D:\tomcat\bin and set the Program/script to restart.bat
, arguments to > "%TEMP%\Temp.log" 2>&1
and Start in to D:\tomcat\bin
.
Listing of restart.bat:
powershell -Command "Restart-Service Tomcat6"
I get the following in C:\Users\tomcat.restarter\AppData\Local\Temp\Temp.log
I get the following content:
D:\tomcat\bin>powershell -Command "Restart-Service Tomcat6"
Restart-Service : Cannot open Tomcat6 service on computer '.'.
At line:1 char:16
+ Restart-Service <<<< Tomcat6
+ CategoryInfo : NotSpecified: (:) [Restart-Service], InvalidOper
ationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Power
Shell.Commands.RestartServiceCommand
Why does powershell -Command "Restart-Service Tomcat6"
fail when run from a Scheduled Task?