Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
}
@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