Skip to content

Instantly share code, notes, and snippets.

@ethzero
Forked from sunnyone/ApacheLogParser.psm1
Last active March 17, 2024 23:44
Show Gist options
  • Save ethzero/cb6eb0476da26ef7b1b629b6eba6d880 to your computer and use it in GitHub Desktop.
Save ethzero/cb6eb0476da26ef7b1b629b6eba6d880 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: $_"
# }
# 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