-
-
Save ethzero/cb6eb0476da26ef7b1b629b6eba6d880 to your computer and use it in GitHub Desktop.
Apache Log Parser for PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Read-ApacheLog | |
{ | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$Path | |
) | |
Get-Content -Path $Path | Foreach-Object { | |
# combined format | |
# if ($_ -notmatch "^(?<Host>.*?) (?<LogName>.*?) (?<User>.*?) \[(?<TimeString>.*?)\] `"(?<Request>.*?)`" (?<Status>.*?) (?<BytesSent>.*?) `"(?<Referer>.*?)`" `"(?<UserAgent>.*?)`"$") { | |
# throw "Invalid line: $_" | |
# } | |
# common format | |
if ($_ -notmatch "^(?<Host>.*?) (?<LogName>.*?) (?<User>.*?) \[(?<TimeString>.*?)\] `"(?<Request>.*?)`" (?<Status>.*?) (?<BytesSent>.*?)$") { | |
throw "Invalid line: $_" | |
} | |
$entry = $matches | |
$entry.Time = [DateTime]::ParseExact($entry.TimeString, "dd/MMM/yyyy:HH:mm:ss zzz", [System.Globalization.CultureInfo]::InvariantCulture) | |
if ($entry.Request -match "^(?<Method>.*?) (?<Path>.*?) (?<Version>.*)$") { | |
$entry.Method = $matches.Method | |
$entry.Path = $matches.Path | |
$entry.Version = $matches.Version | |
} | |
return New-Object PSObject -Property $entry | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment