Emailing collected eventlog data with PowerShell

downloadToday I am helping an IT department who want a daily insight on what is happening on their Citrix XenApp environment. They don’t want to click around to get there information, they just want the information they need in there mailbox every day. To help them I wrote the PowerShell script you see below. So what does it do?

The script collects the application eventlog from 42 XenApp servers. It filters out the errors and sort them by the time they occur. By sorting the events by time it is possible to see trends in the events. This is the code to collect the eventdata:

 Get-Eventlog Application -newest 10 -EntryType Error -computer (get-content d:\eventlogs\servers.txt) | Select MachineName,EventID,Message,Source,TimeGenerated | ConvertTo-Html -head $a

The rest of the script sends the output via an email message and it formats the eventlog messages.

This is the whole script:

 Function SendMail-Applog {

#Style Format
$a = ""

$msg = Get-Eventlog Application -newest 10 -EntryType Error -computer (get-content d:\eventlogs\servers.txt) | Select MachineName,EventID,Message,Source,TimeGenerated | ConvertTo-Html -head $a

Send-MailMessage -To sysadmins@mydomain.nl -Subject "Rapport System Log" -From noreply-rapport@mydomain.nl -bodyasHTML -Body ($msg | Out-String) -SmtpServer c-ex01.mydomain.local -Priority High

}

SendMail-Applog 

*Edit, optimized the PowerShell script after some great feedback from the community