Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
powershell script for emailing eventlog errors
$EventId = 1000
$A = Get-WinEvent -MaxEvents 1 -FilterHashTable @{Logname = "Application"; ID = $EventId}
$Message = $A.Message
$EventID = $A.Id
$MachineName = $A.MachineName
$Source = $A.ProviderName
$rightNow = Get-Date -Format 'MM/dd/yyyy hh:mm:ss'
$EmailTo = "to@other.com"
$EmailFrom = "from@gmail.com"
$Subject = "$($MachineName.Split('.')[0]) Error (Application | $($rightNow))"
$Body = "EventID: $EventID`n`nSource: $Source`n`nMachineName: $MachineName`n`nMessage: $($Message)"
$SMTPServer = "smtp.gmail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(...)
$SMTPClient.Send($SMTPMessage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment