Scheduled Task that runs a PowerShell script returning 0x80070001
I've recently migrated several scheduled tasks from one machine to another as part of a rationalisation process. A couple of these tasks were ones that can only be run at a certain time, so were left to run according to their schedule. They also happened to be the only scheduled tasks that trigger PowerShell scripts.
Both tasks had return codes of 0x80070001 showing in the Scheduled Tasks console and the expected outputs and log files hadn't been created, other tasks that weren't running PowerShell scripts had run successfully, so a little bit of a mystery, especially as the detail for the Task Completed step was showing:
Task Scheduler successfully finished "{c2763d85-184a-4979-8f5f-07b2f77bda49}" instance of the "\My Scheduled Task" task for user "DOMAIN\username".
Attempting to run the task from a PowerShell console generated:
.\MyScript.ps1 : File E:\Services\Scripts\MyScript.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1 + .\MyScript.ps1 + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
A ha! During configuration of the new server the execution policy wasn't changed using Set-ExecutionPolicy. Changing this allowed the scheduled task(s) to run without issue.