-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
753 additions
and
69 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
# 1.0.0 | ||
|
||
* Created `Import-W3CLog` function for parsing and importing W3C log files. |
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
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
|
||
function Import-W3CLog | ||
{ | ||
<# | ||
.SYNOPSIS | ||
Parses and imports W3C log files. | ||
.DESCRIPTION | ||
The `Import-W3CLog` function parses and imports W3C log files, returning objects representing each line. Pass the | ||
path to the log file to parse to the `Path` parameter, or to parse multiple files, pipe them into the function. | ||
.EXAMPLE | ||
Import-W3CLog -Path log.log | ||
Demonstrates how to parse and import a single W3C log file by passing its path to the `Path` parameter. | ||
.EXAMPLE | ||
Get-ChildItem -Path C:\Inetpub\logs -Filter '*.log' -Recurse | Import-W3CLog | ||
Demonstrates how to parse multiple logs by piping their paths to the `Import-W3CLog` function. | ||
#> | ||
[CmdletBinding()] | ||
param( | ||
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] | ||
[Alias('FullName')] | ||
[String] $Path | ||
) | ||
|
||
process | ||
{ | ||
Set-StrictMode -Version 'Latest' | ||
Use-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState | ||
|
||
$Path = $Path | Resolve-Path | ||
if (-not $Path) | ||
{ | ||
return | ||
} | ||
|
||
$displayPath = $Path | Resolve-Path -Relative | ||
if ($displayPath.StartsWith('..')) | ||
{ | ||
$displayPath = $Path | ||
} | ||
|
||
Write-Verbose $displayPath | ||
|
||
$fields = @() | ||
|
||
$lineNum = 0 | ||
foreach ($line in (Get-Content -Path $Path)) | ||
{ | ||
$lineNum += 1 | ||
|
||
if (-not $line) | ||
{ | ||
continue | ||
} | ||
|
||
if ($line.StartsWith('#')) | ||
{ | ||
if ($line.StartsWith('#Fields: ')) | ||
{ | ||
$fields = $line.Split(' ') | Select-Object -Skip 1 | ||
} | ||
else | ||
{ | ||
Write-Verbose " $($line.Substring(1))" | ||
} | ||
continue | ||
} | ||
|
||
$errMsgPrefix = "$($displayPath) line $($lineNum): " | ||
|
||
$entry = [W3CLogs.LogEntry]::New() | ||
|
||
[String[]]$values = $line.Split(' ') | ||
for ($idx = 0; $idx -lt $values.Length; ++$idx) | ||
{ | ||
$propertyName = $fieldName = $fields[$idx] | ||
if ($script:fieldPropertyMap.ContainsKey($fieldName)) | ||
{ | ||
$propertyName = $script:fieldPropertyMap[$fieldName] | ||
} | ||
else | ||
{ | ||
$entry | Add-Member -Name $fieldName -MemberType NoteProperty | ||
} | ||
|
||
$value = $values[$idx] | ||
if ($value -eq '-') | ||
{ | ||
continue | ||
} | ||
|
||
if ($script:httpMethods.Contains($fieldName)) | ||
{ | ||
$value = [Net.Http.HttpMethod]::New($value) | ||
} | ||
elseif ($script:milliseconds.Contains($fieldName)) | ||
{ | ||
$value = [TimeSpan]::New(0, 0, 0, 0, $value) | ||
} | ||
|
||
try | ||
{ | ||
$entry.$propertyName = $value | ||
} | ||
catch | ||
{ | ||
$msg = "$($errMsgPrefix)Failed to convert $($fieldName) value ""$($value)"": $($_)" | ||
Write-Error $msg -ErrorAction $ErrorActionPreference | ||
} | ||
} | ||
|
||
$entry.DateTime = $entry.Date + $entry.Time | ||
|
||
$hostname = 'example.com' | ||
if ($entry.Host) | ||
{ | ||
$hostname = $entry.Host | ||
} | ||
elseif ($entry.ServerIP -and $entry.ServerIP.AddressFamily -eq [Net.Sockets.AddressFamily]::InterNetwork) | ||
{ | ||
$hostname = $entry.ServerIP.IPAddressToString | ||
} | ||
|
||
$queryString = '' | ||
if ($entry.Query) | ||
{ | ||
$queryString = "?$($entry.Query)" | ||
} | ||
|
||
$stem = $entry.Stem | ||
if (-not $stem.StartsWith('/')) | ||
{ | ||
$stem = "/$($stem)" | ||
} | ||
|
||
$url = "http://$($hostname)$($stem)$($queryString)" | ||
try | ||
{ | ||
$entry.Url = [Uri]::New($url) | ||
} | ||
catch | ||
{ | ||
$msg = "$($errMsgPrefix)Failed to convert ""$($url)"" to a [Uri] object: $($_)" | ||
Write-Error $msg -ErrorAction $ErrorActionPreference | ||
} | ||
|
||
$entry | Write-Output | ||
} | ||
|
||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,39 @@ | ||
TOPIC | ||
about_MODULE_NAME | ||
|
||
SHORT DESCRIPTION | ||
W3CLogs is a PowerShell module for... | ||
# Overview | ||
|
||
LONG DESCRIPTION | ||
The W3CLogs PowerShell module... | ||
The "W3CLogs" module has a single function `Import-W3CLog`, which parses and imports W3C log files. | ||
|
||
# System Requirements | ||
|
||
* Windows PowerShell 5.1 and .NET 4.6.1+ | ||
* PowerShell Core 6+ | ||
|
||
# Installing | ||
|
||
To install globally: | ||
|
||
```powershell | ||
Install-Module -Name 'W3CLogs' | ||
Import-Module -Name 'W3CLogs' | ||
``` | ||
|
||
To install privately: | ||
|
||
```powershell | ||
Save-Module -Name 'W3CLogs' -Path '.' | ||
Import-Module -Name '.\W3CLogs' | ||
``` | ||
|
||
# Usage | ||
|
||
Pass the path to a single log file to parse to the function's `Path` parameter: | ||
|
||
```powershell | ||
Import-W3CLog -Path 'log.log' | ||
``` | ||
|
||
To parse multiple logs, pipe them in: | ||
|
||
```powershell | ||
Get-ChildItem -Recurse -Filter '*.log' | Import-W3CLog | ||
``` |
Oops, something went wrong.