Created
January 13, 2017 22:13
-
-
Save vendettamit/60543e117519245cbd238ab8362b657f to your computer and use it in GitHub Desktop.
Powershell script to run when an event occurs in Windows event log
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
# To test this script you can use Powershell to write your own test error log entry in the following way: | |
# ------------------------------------- | |
# New-EventLog –LogName Application –Source "Test" | |
# Write-EventLog –LogName Application –Source "Test" –EntryType Error –EventID 1 –Message "This is a test message." | |
# Name to filter the event uses wild card pattern matching | |
$a = "*LocalReport*Application*" | |
#Write-EventLog –LogName Application -Source "Test" –EntryType Error –EventID 1046 –Message "This is a test message for $($a)." | |
$event = get-eventlog -LogName Application -newest 1 | |
#write-host $event.Message | |
# "Error" - send only error | |
if ($event.EntryType -eq "Error" -Or $event.EntryType -eq "Critical") | |
{ | |
If($event.Message -like "*$($a)*") | |
{ | |
$PCName = $env:COMPUTERNAME | |
$EmailBody = $event | format-list -property * | out-string | |
$EmailFrom = "$PCName <[email protected]>" | |
$EmailTo = "[email protected]" | |
$EmailSubject = "Error in $($a) program" | |
$SMTPServer = "relay2" | |
Write-host "Sending Email" | |
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -body $EmailBody -SmtpServer $SMTPServer -Priority High | |
} | |
#Check status of all court alert services | |
Invoke-Expression $checkStatusScript | |
} | |
else | |
{ | |
write-host "No error found" | |
write-host "Here is the log entry that was inspected:" | |
$event | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment