Using Windows Server 2012.
I created a powershell script which worked when I ran used Windows Powershell ISE.
But when I scheduled the script to run as a Scheduled Task then it did not work – the “Last Run Result” was (0x1).
This is how I had configured the scheduled task: I created a new scheduled task and in the Actions tab I created a new action with the following properties:
-Program/script: powershell.exe
-Add args(optional): -file "c:\test\myscript.ps1"
-Start in(optional): C:\Windows\System32\WindowsPowerShell\v1.0
This would not work either as a schedule or by clicking “run” from the library list view.
To fix I created the full command as follows and ensured it worked from the Windows “run” command:
powershell.exe -file "c:\test\myscript.ps1"
Then I took the full command string and inserted into the “Program/script” input of the scheduled task action. I deleted the value from “Add args” input. When I clicked OK to save changes, a dialog appeared:
“It appears as though arguments have been included in the Program text box. Do you want to run the following program: powershell.exe
With the following arguments: -file “c:\test\myscript.ps1”
Answer Yes.
Scheduled task now works.