Problem overview
Monitoring Windows event logs can be daunting, especially when you need to isolate specific error or warning occurrences. Missed events can quickly escalate into unresolved issues, leading to service disruptions, user complaints, or system crashes. This script pinpoints crucial log entries automatically, helping IT professionals identify and respond to problems before they spread.
<#
This resource is provided as a convenience for Level users. We cannot
guarantee it will work in all environments. Please test before deploying
to your production environment. We welcome contributions to our community
library
Level Library
https://level.io/library/script-windows-event-log-monitor
#>
#Chose which event log to monitor: application, security, or system
$LogName = "application"
#Chose which event ID to monitor.
$ID = 1000
#Chose the severity level of the event. (Critical 1, Error 2, Warning 3,
#Informational 4) Can be comma seperated list (don't use quotes)
$EventSeverity = 2
#Chose the provider name (source) of the event.
$ProviderName = "Application Error"
#Chose the timeframe (in minutes) in which to search. Search the logs filtered
#to the past X minutes. This should be synced up with the monitor run
#frequency. If the frequency will be set to checking every 5 minutes, then the
#timeframe shouldn't exceed that.
$Timeframe = 5
$TimeSpan = (Get-Date) - (New-TimeSpan -Minutes $Timeframe)
$ErrorActionPreference = 'silentlycontinue'
#Pull the events and filter them
$EventTracker = Get-WinEvent -FilterHashtable @{
LogName = $LogName
ID = $ID
Level = $EventSeverity
ProviderName = $ProviderName
StartTime = $TimeSpan
} -MaxEvents 10
#Display the events
$EventTracker
#If there are events that match, trigger the ALERT
if ($EventTracker) {
Write-Output "ALERT"
exit 1
}
else {
Write-Output "Events not found. Check your filter variables if you are expecting a match."
exit 0
} This script filters the specified Windows event log—whether Application, Security, or System—for event IDs, severity levels, and provider names you define. It also allows you to focus only on events within a designated timeframe, ensuring timely and relevant alerts. If matching entries are found, the script triggers an alert exit code (1), facilitating an automated response or investigation through Level’s integrated monitoring and alerting features.
Use cases
- Pinpointing recurrent crash events from a specific application
- Tracking security or system warnings within critical timeframes
- Identifying serious application errors before they impact end users
- Triggering alert-driven automations for immediate incident response
Recommendations
- Configure a script-based monitor in Level to run at intervals matching your timeframe setting
- For recurring checks, create an automation in Level with a scheduled trigger that runs this script to regularly scan event logs
- Test in a non-production environment to confirm correct event filtering and avoid false positives
- Update the script variables (LogName, ID, EventSeverity, ProviderName, Timeframe) to suit your specific monitoring needs
- Keep an eye on performance by limiting the timeframe and maximum events to reduce overhead on busy systems