Powershell script to email event log error count

by sunil ravulapalli /23. May 2013 00:05 /powershell /Comments (0)

Below is the powershell script to email a summary of event log error count for the last four hours as soon as the script is executed. You can schedule a windows task to execute this script periodically.

$smtp_server = "mail.xxxxxx.com"
$host_name = [System.Net.Dns]::GetHostName()
$from_email =  $host_name + "@xxxxxx.com"
$to_emails = @("one@xxxxxx.com, two@xxxxxx.com")
$apps = @("App1", "App2")
$errors_end_datetime = Get-Date
$errors_start_datetime = $errors_end_datetime.AddHours(-4)

function sendMail($content)
{
     #SMTP server name
     $smtpServer = $smtp_server

     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage

     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)

     #Email structure 
     $msg.From = $from_email
	 foreach ($to_email in $to_emails)
	 {
		$msg.To.Add($to_email)
	 }
     $msg.subject = "Error Summary after: " + $errors_start_datetime + ". Host: " + $host_name
     $msg.body = $content
	 $msg.IsBodyHtml = $true
     #Sending email 
     $smtp.Send($msg)
}

$email_content = "
"; $email_content += ""; $email_content += "" + "" + "" + "" foreach ($app in $apps) { $error_count = 0; $error_count = (Get-EventLog $app | Where-Object {$_.EntryType -eq "Error" -and $_.TimeWritten -gt $errors_start_datetime}).count $email_content += "" + "" + "" + "" } $email_content += "
" + "App Name" + " " + "No. of errors" + "
" + $app + " " + $error_count + "
"; $email_content += "
";
sendMail $email_content