2

I'm trying to run a PowerShell script every minute using Task Scheduler. The script basically fires off an HTTP GET request and I want to have the task show the "Run Result" as an error if the request is not successful.

Right now, no matter what happens in my script, the Task Scheduler is showing the last "Run Result" as "Success", and I'm not sure what I should be doing to make it work properly. In my script, I am using Exit 1 if the request does not return a 200/OK, and Exit 0 otherwise.

My Task action is set up as follows:

  • Action: Start a program
  • Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • Add arguments: -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command "C:\MyScript.ps1; Exit $LASTEXITCODE"
  • Start in: C:\
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user1496624
  • 107
  • 1
  • 2
  • 12
  • The task should work as you expect (although I'd use `-File "C:\MyScript.ps1"` instead of `-Command`). Did you refresh the view? The Task Scheduler UI can be a little unresponsive at times. – Ansgar Wiechers Jul 31 '15 at 16:10
  • @AnsgarWiechers I'm noticing that in the "Task Status" section of the "Task Scheduler (Local)" view, when I expand my job to see the past executions, the "Run Result" is "Success" every time. However, if I go to the "Task Scheduler Library" view, I see that the "Last Run Result" is (0x1) which appears to be the correct error code coming out. I'm not sure what the difference is between those two views, but my question was geared towards that first view I described. – user1496624 Jul 31 '15 at 16:41

2 Answers2

1

You are running everything correctly, and have everything set up correctly.

To answer your question, the "Task Scheduler Library" view, "Last Run Result" is the correct result, and the correct View to see if things ran successfully or not.

The "Task Status" section of the "Task Scheduler (Local)" view is just purely telling you whether or not the Task ran or not. It does not return the results of the Task Actions. So it will only show an error if something caused it to miss it's scheduled running (for ex. going on battery power), and not if the Task Action failed.

Personally I always use the "Task Scheduler Library" view, "Last Run Result" as the only way to see whether or not things failed or not.

HAL9256
  • 12,384
  • 1
  • 34
  • 46
  • I am sorry, @AnsgarWiechers, you are right. I was thinking of this issue at the time: (http://stackoverflow.com/questions/18410956/powershell-commands-exit-code-is-not-the-same-as-a-scripts-exit-code) where `-Command` won't return the correct exit code (and hence the ugly workaround) and incorrectly was attributing it to the problem being with `-File`. I have removed the comments about it. – HAL9256 Aug 04 '15 at 17:58
0

You can use EXIT %ERRORLEVEL% in cmd file to "bubble up" any non-zero return code from PowerShell to the Last Run Result column in Task Scheduler

as described in http://www.technologytoolbox.com/blog/jjameson/archive/2011/11/19/tips-tricks-for-running-powershell-scripts-as-scheduled-tasks.aspx

chunhunghan
  • 51,087
  • 5
  • 102
  • 120