Skip to content

Instantly share code, notes, and snippets.

@LeeHolmes
Created September 10, 2022 21:31
Show Gist options
  • Save LeeHolmes/53bbb4da21c9752e12575356536136cb to your computer and use it in GitHub Desktop.
Save LeeHolmes/53bbb4da21c9752e12575356536136cb to your computer and use it in GitHub Desktop.
Tail an event log through PowerShell
## PowerShell Eventing lets you tail an event log:
## http://powershellcookbook.com/recipe/IMyz/respond-to-automatically-generated-events
$watcher = New-Object System.Diagnostics.Eventing.Reader.EventLogWatcher "Microsoft-Windows-PowerShell/Operational"
Register-ObjectEvent $watcher EventRecordWritten -Action {
$event = $eventArgs.EventRecord
if($event.ProcessId -ne $pid)
{
## Save the last event into a variable in the PowerShell sesssion if you want to explore its properties,
## as the eventing actions run in their own runspace
# $GLOBAL:lastEvent = $event
$event | Select Id, LogName, TimeCreated, @{ Label = "Message"; Expression = { $_.FormatDescription() } } |
Format-Table -Wrap | Out-Host
}
}
$watcher.Enabled = $true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment