Skip to content

Instantly share code, notes, and snippets.

@iyevhen
Created April 9, 2021 11:14
Show Gist options
  • Save iyevhen/bac257ca30fc5d07beea26ce563c04e8 to your computer and use it in GitHub Desktop.
Save iyevhen/bac257ca30fc5d07beea26ce563c04e8 to your computer and use it in GitHub Desktop.
Query All Event Logs for recent events for last hour using PowerShell
$old = (Get-Date).AddHours(-1)
$before_lines = 50
$after_lines = 50
Write-Host "Pulling Warn/Error Windows Events started >= $old"
Get-WinEvent -ListLog * -EA silentlycontinue |
Where-Object { $_.recordcount -AND $_.lastwritetime -gt $old } |
ForEach-Object { get-winevent -FilterHashtable @{LogName=$_.logname; StartTime=$old } -EA silentlycontinue } |
Sort-Object TimeCreated |
Format-List TimeCreated, LevelDisplayName, ProviderName, Message |
Out-String -Stream -Width 1000 |
Select-String -Pattern 'LevelDisplayName\s+:\s+Error.*','LevelDisplayName\s+:\s+Warning.*' -Context $before_lines,$after_lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment