Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Created November 3, 2015 11:32
Show Gist options
  • Save tugberkugurlu/8badc5bca409989674fd to your computer and use it in GitHub Desktop.
Save tugberkugurlu/8badc5bca409989674fd to your computer and use it in GitHub Desktop.
collect event logs with PowerShell
$ErrorActionPreference = 'Stop'
# Get-EventLog -List = get all the EventLog
# Get-EventLog system -Newest 3 = get latest 3 event logs
# Get-EventLog application | where { $_.Source -eq ".NET Runtime" } | select TimeWritten
$today = [DateTime]::UtcNow.Date
$todayMarker = $today.ToString("yyyy-MM-dd")
$file = "application-dotnet-runtime-$todayMarker"
$todayFilePath = "$PSScriptRoot\$file.txt"
if(-not (Test-Path $todayFilePath)) {
New-Item $todayFilePath -ItemType File
}
## TODO: Get this based on last collected log
$lastDate = [DateTime]::Parse("10/19/2015 3:17:27 PM")
$todayLast = $today.AddHours(23).AddMinutes(59).AddSeconds(59).AddMilliseconds(999)
Get-EventLog application |
where { $_.Source -eq ".NET Runtime" -and $_.TimeWritten -gt $lastDate -and ($_.TimeWritten -lt $todayLast -or $_.TimeWritten -eq $todayLast) } |
foreach {
Write-Output $_.Message
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment