Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Powershell script to run when an event occurs in Windows event log
# 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)*")
$EmailBody = $event | format-list -property * | out-string
$EmailFrom = "$PCName <>"
$EmailTo = ""
$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
write-host "No error found"
write-host "Here is the log entry that was inspected:"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.