I have a script that just calls Exit 1
. But, when this script is run via Windows Task Scheduler, I get either a return code of 0
, or 2147942401
, but I never get the expected 1
. Details below.
Script Content
Exit 1
Windows Task Definition:
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -NoProfile -ExecutionPolicy ByPass -Command '. C:\myscript.ps1; exit $LastExitCode'
Run Result
When I run this task, the task history shows, Task Scheduler successfully completed task "\My task" , instance "{3f344413-46c2-4419-b46b-85896f241d60}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe" with return code 0.
If I alter the Windows Task definition to use double quotes instead of single quotes, I get a different result.
Edited Windows Task Definition
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -NoProfile -ExecutionPolicy ByPass -Command ". C:\myscript.ps1; exit $LastExitCode"
New Result
Task Scheduler successfully completed task "\My task" , instance "{c44082a8-56fe-4615-aad0-70dca8b71881}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe" with return code 2147942401.
How can I get 1
for my task's return code? Do I need to run 2147942401
through some kind of convertor to get 1
? Or is there something else at work here?
Thanks.