Forum Discussion
Nicola1976
Apr 26, 2023Copper Contributor
script for failed task scheduler
Hello everyone, first of all, im not a programmer 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 (f...
AndySvints
May 01, 2023Steel Contributor
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.
- Nicola1976May 02, 2023Copper Contributor
AndySvints works very well, thank you!