Skip to content

Instantly share code, notes, and snippets.

@sunnyone
Created December 15, 2012 03:56
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sunnyone/4291248 to your computer and use it in GitHub Desktop.
Apache Log Parser for PowerShell
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
}
}
@scotthermes
Copy link

Works great! Thanks!

@bishgupp
Copy link

bishgupp commented Dec 8, 2014

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