Public/ConvertFrom-NSPW3CLog.ps1
|
function ConvertFrom-NSPW3CLog { <# .SYNOPSIS Reads a whitespace-delimited W3C log using its #Fields header. #> [CmdletBinding()] param([Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$Path) $resolved = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).ProviderPath $fieldNames = $null $lineNumber = 0 foreach ($line in [IO.File]::ReadLines($resolved)) { $lineNumber++ if ([string]::IsNullOrWhiteSpace($line)) { continue } if ($line -match '^#Fields:\s*(.+)$') { $fieldNames = @($Matches[1].Trim() -split '\s+') if ($fieldNames.Count -ne @($fieldNames | Select-Object -Unique).Count) { throw "Duplicate W3C field name at line $lineNumber." } continue } if ($line.StartsWith('#')) { continue } if (-not $fieldNames) { throw "No #Fields header before data at line $lineNumber." } $values = @($line.Trim() -split '\s+') if ($values.Count -ne $fieldNames.Count) { throw "Expected $($fieldNames.Count) fields, got $($values.Count) at line $lineNumber." } $record = [ordered]@{} for ($i = 0; $i -lt $fieldNames.Count; $i++) { $record[$fieldNames[$i]] = $values[$i] } [pscustomobject]$record } } |