none
Send Email if Task ends with Error Code other than 0x0

    Question

  • Good day

    We have an Windows 7 workstation on which do run several Scheduled Tasks. All taks are created in a separated "folder" in the scheduled Task section. (For Example Folder "Test")

    What i should be able to do, is to create a script (best thing maybe Powershell) which does check all those Tasks in this particular "folder" and if the "Last Result" Code is different than 0 (or 0x0) then an email should be sent.

    I was able to read the TaskResult with the following line

    Get-ScheduledTask -TaskPath \Test\ | Get-ScheduledTaskInfo | where {$_.LastTaskResult -ne "0"} | fl Name,LastTaskResul

    but i dont know what to do next (how to read the output and send an email)

    I'm not into scripting so please has someone an advice or a script for this problem?

    thanks

    andre

    Wednesday, October 02, 2013 5:56 AM

Answers

  • Thanks for the reply

    have found the powershell pack (http://blog.powershell.no/2012/05/28/working-with-scheduled-tasks-from-windows-powershell/). it delivers something like the get-scheduledTask cmdlet.

    Now i figured out the command, and it looks like this

    Get-ScheduledTask -folder <folder> | where {$_.Status -ne “Disabled” -and $_.Name -ne “_Scheduled-Task-Monitor”} | ForEach-Object {if ($_.LastTaskResult -ne “0″){Send-MailMessage -body $_.Name -SmtpServer <mail.server.local> -From <sender@domain.local> -To <recipient@domain.local> -Subject “<Subject>”}}

    tested it and it seems to work

    Thursday, October 03, 2013 10:24 AM

All replies

  • You could place the executable for each task into a batch file like so:

    @echo off
    "c:\My Folder\My App.exe" parm1 parm2 parm3
    if %ErrorLevel% NEQ 0 c:\Tools\blat.exe -server ... -to ...

    You can download blat.exe from a number of sites.

    Wednesday, October 02, 2013 7:45 AM
  • Hi

    Thanks for your reply. Sounds like good idea. But i have a lot of scheduled task which i have to edit....

    In the meantime i have played a bit arround with powershell. this seems to work as well:

    Get-ScheduledTask -TaskPath \TEST\ | Get-ScheduledTaskInfo | ForEach-Object {if ($_.LastTaskResult -ne "0"){Send-MailMessage -body "$_.TaskName" -SmtpServer <smtpserver> -From <from> -To <to> -Subject "Task did not Run - Please Check!!!"}}

    regards

    andre

    Wednesday, October 02, 2013 8:41 AM
  • hmm have just discovered that "get-scheduledtask" is not recognized in windows  7... only windows 8 and server 2012... is there any way to get this command work in windows 7? thank
    Wednesday, October 02, 2013 10:40 AM
  • hmm have just discovered that "get-scheduledtask" is not recognized in windows  7... only windows 8 and server 2012... is there any way to get this command work in windows 7? thank
    Best to ask the experts in the Script Center.
    Wednesday, October 02, 2013 4:06 PM
  • Hi,

    Get-ScheduledTask cmdlet applies to windows server 2012 and windows 8, it’s a newer function which does not exists in previous PowerShell.

    Forest brook is right, you can ask a question in script center to get more help on script issue.

    If you have any feedback on our support, please click here

    Alex Zhao
    TechNet Community Support

    Thursday, October 03, 2013 2:02 AM
  • Thanks for the reply

    have found the powershell pack (http://blog.powershell.no/2012/05/28/working-with-scheduled-tasks-from-windows-powershell/). it delivers something like the get-scheduledTask cmdlet.

    Now i figured out the command, and it looks like this

    Get-ScheduledTask -folder <folder> | where {$_.Status -ne “Disabled” -and $_.Name -ne “_Scheduled-Task-Monitor”} | ForEach-Object {if ($_.LastTaskResult -ne “0″){Send-MailMessage -body $_.Name -SmtpServer <mail.server.local> -From <sender@domain.local> -To <recipient@domain.local> -Subject “<Subject>”}}

    tested it and it seems to work

    Thursday, October 03, 2013 10:24 AM