PowerShell and Task Scheduler – Working with Exit Code (Last Run Result)

Until we start using PS 3.0, we’re stuck with using Task Scheduler to run our tasks, which is fine, but its hard to tell when a script fails. We then need to create monitoring scripts to monitor the results of the script, or add a lot to the script to get it to handle its own errors.

If you have a monitoring system in place that can monitor schedule tasks, then it would be great to put that to use, and now you can.

In any place you’d normally trap errors and exit, you can do this.


This will send the Exit Code 11 to Task Scheduler so that a monitoring system can pull the last run result from that task.

There is a ExitCode property in Environment which sets the code on a normal exit


So you can set it and let the script continue if you like.

[Environment]::ExitCode = 11

The other part of this is reading exit codes from task scheduler (maybe you want to make your own monitoring system)

Its real easy if you have the task name, lets say the task is called TestScript

schtasks /query /tn TestScript /v /fo list | ?{$_ -match "Last Result:\s+(.+)"} | %{$matches[1]}

You can remove the TN param and get info for all but then you need to do a little more regex work to pull what you need, but it’s not all that hard.

If you are interested in getting all of them let me know!



About jrich

I am the Solutions Architect for Apex Learning in Seattle WA. I've been working with computers since I was 13. Started programming when I was 14. Had my first IT job as tech support at an ISP at the age of 15 and became a network admin at the age of 17. Since then I've worked at a variety of small to mid size companies supporting, maintaining and developing all aspects of IT. Mostly working with Windows based networks but have recently been working with Solaris system as well. I created this blog mostly as a place for me to take my own notes, but also share things that I had a hard time finding the info for.

Posted on October 6, 2011, in WMF (Powershell/WinRM) and tagged , , , , , , . Bookmark the permalink. 4 Comments.

  1. Please can you provide me with how to pull this for all tasks?

    Kind regards,

  2. you’d use this command
    schtasks /query /v /fo list
    and then you’d need to process each line. in the code above I searched for a certain task and its status. so you’ll need to break out the results with regex. The other option is to use this module, which does all that work for you.

  3. Is there any way to exit with text instead of just a number? Where Microsoft puts “The operation has completed successfully” It’d be nice to have a text string.

    It’s not a big deal, really, but it’d be nice.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: