Send Windows Event Logs as an e-mail. (PowerShell)

This script can be used to send windows event logs on period basis to specific e-mail. Below example sending last 24 hours of events in the Windows Essential Licensing section.

$Yesterday = (Get-Date) - (New-TimeSpan -Day 1)
$logs = Get-WinEvent -LogName "Microsoft-Windows-Server Infrastructure Licensing/Operational" | Where-Object {$_.TimeCreated -ge $Yesterday} | Format-List | Out-String
$username = "user@domain.com"
$password = "mypassword"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
#Send-MailMessage [-Attachments ] [-Bcc ] [[-Body] ] [-BodyAsHtml]
# [-Encoding ] [-Cc ] [-DeliveryNotificationOption ]
# -From  [[-SmtpServer] ] [-Priority ] [-Subject]  [-To] 
# [-Credential ] [-UseSsl] [-Port ] []
Send-MailMessage -Body $logs `
-From me@domain.com `
-SmtpServer mail.server.com `
-Subject "Licensing Logs" `
-To "user@domain.com" `
-UseSsl -Credential $cred

Leave a Reply