I have a PowerShell script (that works). In Windows Task Scheduler I created a new task to execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
, passing the argument as my PS1 script. When the task runs I get a Last Run Result of 0x1
.
I updated my script to write to a log file when the script opens and that isn't happening. It's almost like the task can't even open Powershell.exe.
Does this sound accurate? What could the issue be or how do I work around it?
It could be PowerShell's default security level, which (IIRC) will only run signed scripts. That will tell PowerShell to allow local (that is, on a local drive) unsigned scripts to run. Then try executing your script again. You have to run Powershell with administrator privileges, at least under Windows 8!
If the Task Scheduler stops automatically or is not running on your Windows 10 system, you need to do the following things: Check and start Task Scheduler service. Change the Service configuration. Start Task Scheduler using Command Prompt.
If the problem you're having is with Execution Policy, then you can also set the execution policy of a specific invocation of PowerShell. This is what I usually do when executing PowerShell through a scheduled task:
powershell.exe -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File \\path\to\script.ps1
This ensures that you don't rely on anything in the user's PowerShell profile, and avoids the overhead of executing that additional code.
This mostly doesn't matter; maybe it does if you're capturing the output of your script. Mostly it makes me feel better.
Ensures that your task won't wait indefinitely if something in your script unexpectedly prompts the user. With this switch, the script will just exit instead; at least you'll have an error code instead of a hanging script.
You can use Unrestricted
here or whichever execution policy you like. This is probably the one you need the most.
Because I don't want the task to depend on a global non-default setting that you may have other reasons to change in the future. If some other process depends on a different execution policy, then it's not at odds with your task this way.
Plus it's always nice not to have to change the defaults. Less to remember/document/test.
See JohnLBevan's answer for some additional causes of 0x1
result in a scheduled task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With