Created
December 15, 2012 03:56
-
-
Save sunnyone/4291248 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: $_" | |
} | |
$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 | |
} | |
} |
Great script. Thanks for posting.
What is the best way to modify it for apache\access.log?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works great! Thanks!