Created
March 26, 2021 16:10
-
-
Save cemerson/2a7e3961cdc09f5edd5d185c8c68d4fb to your computer and use it in GitHub Desktop.
powershell script for emailing eventlog errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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