script for failed task scheduler

Copper Contributor

Hello everyone,

first of all, im not a programmer :flushed: so sorry for any stupid thing i said, in advance

 

I've "made" a script that alert me via mail if a scheduled task gives a specific ID event (for example ID 322 instance already running) on the company's server. This is because we have an app that run via task scheduler every 5 minutes, but sometimes the app freeze, so task scheduler does not run it again because it's already running (but not working).

 

 

 

$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = "smtp.gmail.com"
$SmtpClient.Port = 587
$smtpclient.EnableSsl = $true
$mailmessage.from = ("email address removed for privacy reasons")
$mailmessage.To.add("email address removed for privacy reasons")
$mailmessage.Subject = “Import Server Alert ID322”
$mailmessage.Body = “Server Dat-TaskScheduler-Evento 322. Richiesta di avvio ignorata. Istanza già in esecuzione”
$smtpclient.Credentials = New-Object System.Net.NetworkCredential("email address removed for privacy reasons", "password")
$smtpclient.Send($mailmessage)

 

 

 

It work, but i'd like to add on the mail's body the result of get-WinEvent so it can show me which task "failed".

 

 

 

Get-WinEvent -MaxEvents 1 -FilterHashtable @{ logname='microsoft-windows-taskscheduler/operational';ID=322}  | 
    Select @{n='Time';e={$_.TimeCreated}},
           @{n='Source';e={$_.ProviderName}},
           @{n='EventId';e={$_.Id}},
           @{n='ResultCode';e={$_.Properties.Value[2]}},
           @{n='Message';e={$_.Message}},
           @{n='EventLog';e={$_.LogName}}

 

 

 

If possible, it would also be nice to have a single script that alert me on more than one ID event.

Thank to everyone in advance and to the ones i copied the script :flushed:

(mostly https://superuser.com/questions/249103/make-windows-task-scheduler-alert-me-on-fail and https://social.technet.microsoft.com/Forums/en-US/3615923e-8a51-49f4-aedd-b595cf960a6d/extracting-da...

 

3 Replies

Hi @Nicola1976 

AndresBohren_2-1682632902189.png

 

Click through the wizard and then set your Trigger and as Action use your SendMail Script

 

AndresBohren_4-1682632926307.png

AndresBohren_3-1682632914232.png

AndresBohren_1-1682632875550.png

 

Hello @Nicola1976,

One of the possible options is to add table and format your email in HTML.

Something along those lines:

$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = "smtp.gmail.com"
$SmtpClient.Port = 587
$smtpclient.EnableSsl = $true
$mailmessage.from = ("email address removed for privacy reasons")
$mailmessage.To.add("email address removed for privacy reasons")
$mailmessage.Subject = “Import Server Alert ID322”

#Render email as HTML
$mailmessage.IsBodyHTML=$true

#Get your event
$Event=Get-WinEvent -MaxEvents 1 -FilterHashtable @{ logname='microsoft-windows-taskscheduler/operational';ID=322}

#Create simple HTML table
$mailmessage.Body=@"
<p>Server <b>Dat-TaskScheduler-Event 322</b>. Richiesta di avvio ignorata. Istanza già in esecuzione.
Si prega di rivedere i dettagli di seguito
</p>
<table style="width:100%">
  <tr>
    <th>Time</th>
    <th>Source</th>
    <th>EventId</th>
    <th>ResultCode</th>
     <th>Message</th>
     <th>EventLog</th>
  </tr>
<tr>
 <td>$($Event.TimeCreated)</td>
 <td>$($Event.ProviderName)</td>
 <td>$($Event.Id)</td>
 <td>$($Event.Properties.Value[2])</td>
 <td>$($Event.Message)</td>
 <td>$($Event.LogName)</td>
</tr>
</table>
"@


$smtpclient.Credentials = New-Object System.Net.NetworkCredential("email address removed for privacy reasons", "password")
$smtpclient.Send($mailmessage)

 

Hope that helps.

@AndySvints works very well, thank you!