Skip to content

Instantly share code, notes, and snippets.

@jstangroome
Created August 8, 2013 23:06
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jstangroome/6189660 to your computer and use it in GitHub Desktop.
Save jstangroome/6189660 to your computer and use it in GitHub Desktop.
Function to convert lines in an IIS W3C log file to PowerShell objects
function ConvertFrom-IISW3CLog {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('PSPath')]
[string[]]
$Path
)
process {
foreach ($SinglePath in $Path) {
$FieldNames = $null
$Properties = @{}
Get-Content -Path $SinglePath |
ForEach-Object {
if ($_ -match '^#') {
#metadata
if ($_ -match '^#(?<k>[^:]+):\s*(?<v>.*)$') {
#key value pair
if ($Matches.k -eq 'Fields') {
$FieldNames = @(-split $Matches.v)
}
}
} else {
$FieldValues = @(-split $_)
$Properties.Clear()
for ($Index = 0; $Index -lt $FieldValues.Length; $Index++) {
$Properties[$FieldNames[$Index]] = $FieldValues[$Index]
}
[pscustomobject]$Properties
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment