# Parse-ApacheLog # # Reads Apache combined format Logs from STDIN and reports them as Object for further processing # # Based on sunnyone/ApacheLogParser.psm1 https://gist.GitHub.com/sunnyone/4291248 # 1und1 Hinweise zu Log-Dateien https://www.ionos.de/hilfe/hosting/log-dateien/hinweise-zu-log-dateien/ # Apache LogFiles Log Files http://httpd.apache.org/docs/current/logs.html param ( [ValidateSet('common', 'combined', '1und1')] [string]$format="1und1" ) begin { write-verbose "Parse-ApacheLog: Start" switch($format) { "common" {$regex="^(?.*?) (?.*?) (?.*?) \[(?.*?)\] `"(?.*?)`" (?.*?) (?.*?)$" } "combined" {$regex="^(?.*?) (?.*?) (?.*?) \[(?.*?)\] `"(?.*?)`" (?.*?) (?.*?) `"(?.*?)`" `"(?.*?)`"$" } "1und1" {$regex="^(?.*?) (?.*?) (?.*?) \[(?.*?)\] `"(?.*?)`" (?.*?) (?.*?) (?.*?) `"(?.*?)`" `"(?.*?)`"$" } } } Process { if ($_ -notmatch $regex) { write-verbose " Skip invalid Line $($_)" } else { $entry = $matches $entry.Time = [DateTime]::ParseExact($entry.TimeString, "dd/MMM/yyyy:HH:mm:ss zzz", [System.Globalization.CultureInfo]::InvariantCulture) if ($entry.Request -match "^(?.*?) (?.*?) (?.*)$") { $entry.Method = $matches.Method $entry.Path = $matches.Path $entry.Version = $matches.Version } # Convert Hashtable to Object return New-Object PSObject -Property $entry } } end { write-verbose "Parse-ApacheLog: Start" }