PowerShell script won't execute as a Windows scheduled task

后端 未结 5 1369
难免孤独
难免孤独 2020-12-02 14:12

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\"

5条回答
  •  心在旅途
    2020-12-02 14:59

    There are several possible causes for a PowerShell script invoked by the task scheduler to complete with code 0x1:

    • The execution policy does not allow the script to run. See Briantist's excellent answer for detail on this.
    • The task does not have the Run with highest privileges flag (checkbox on the task's General tab) enabled.*
    • Parameters are being passed to the script incorrectly. If using an approach such as -File ".\MyScript.ps1" -Parameter1 'Demo', instead try: -Command "& .\MyScript.ps1 -Parameter1 'Demo'"

    *As Ben points out in the comments, Run with highest privileges doesn't have to be enabled / it depends if the script requires admin rights (e.g. if some commands like Set-ExecutionPolicy or Stop-Process may require these). If you're not sure, try ticking the option to see if it fixes your issue; if it doesn't seem to make a difference, leave it unchecked.

提交回复
热议问题