I've setup a scheduled task on a Windows 2008 Server with two actions. The first action is to run an application which connects to a SQL database and generates XML files. The second action is to run a Powershell script. Provided the task starts correctly the script simply sends a mail message saying that the task has started, and attaches the latest log file created by the data extraction application.
If the scheduled task doesn't start (it didn't last night, and the last run result indicated it was because 'An instance of this task is already running') the Powershell script doesn't fire, so we don't get an email. This is a quick indicator that the task hasn't run, however I'd like the Powershell script to return the Last Run Result from the task scheduler if the task doesn't run (although everytime would be fine). I'm a beginner with Powershell, and I've only scratched the surface with it, so is this possible, or am I asking the impossible? Is there another, glaringly obvious way of doing this? Should I just have a scheduled task with a single action to run a Powershell script which runs the application, and fires off a notification email with the log / last run details?
I've figured out a way to achieve this. Firstly I found this article Monitoring last result of scheduled tasks on Windows. This contained a script written in Perl on how to obtain the Last Run Result of a scheduled task.
I set about converting it to Powershell and this is what I got
$taskName = "ETL" $result = (schtasks /query /FO LIST /V /TN $taskName | findstr "Result") $result = $result.substring(12) $lastResult = $result.trim()
Then I simply insert the value into the email notification.