Skip to content

Instantly share code, notes, and snippets.

@vendettamit
Created January 13, 2017 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vendettamit/60543e117519245cbd238ab8362b657f to your computer and use it in GitHub Desktop.
Save vendettamit/60543e117519245cbd238ab8362b657f to your computer and use it in GitHub Desktop.
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)*")
{
$PCName = $env:COMPUTERNAME
$EmailBody = $event | format-list -property * | out-string
$EmailFrom = "$PCName <Test@cshandler.com>"
$EmailTo = "ITSupport@cshandler.com"
$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