Created
August 19, 2012 08:22
-
-
Save hidori/3393641 to your computer and use it in GitHub Desktop.
Get-IisLog.ps1
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
Param( | |
[Parameter(Mandatory=$True,Position=0)] | |
[String[]] | |
$Path, | |
[Switch] | |
$Utc) | |
Function FindIndex | |
{ | |
Param ( | |
[String[]] | |
$Source, | |
[String] | |
$Value) | |
for ($Index = 0; $Index -lt $Source.Length; $Index++) | |
{ | |
if ($Source[$Index] -eq $Value) | |
{ | |
return $Index | |
} | |
} | |
-1 | |
} | |
foreach($LogFile in $(Get-Item -Path $Path)) | |
{ | |
$LineNumber = 0 | |
$Field = @() | |
$IndexOfDate = -1 | |
$IndexOfTime = -1 | |
Get-Content -Path $LogFile | | |
%{ | |
$LineNumber++ | |
[String[]]$Column = $([String]$_).Split(' ') | |
if (($Column.Length -gt 0) -and (-not [String]::IsNullOrEmpty($Column[0]))) | |
{ | |
if ($Column[0] -ieq "#Fields:") | |
{ | |
$Field = $Column[1..$($Column.Length - 1)] | %{ [String]::Join("", $($_.Split(@('-'; '('; ')')) | %{ $(Get-Culture).TextInfo.ToTitleCase($_) })) } | |
$IndexOfDate = FindIndex -Source $Field -Value "Date" | |
$IndexOfTime = FindIndex -Source $Field -Value "Time" | |
} | |
elseif (($Column[0][0] -ne "#") -and ($Column.Length -ge $Field.Length)) | |
{ | |
$Object = New-Object -TypeName PSObject | |
[Void](Add-Member -InputObject $Object -Type NoteProperty -Name Path -Value $([System.IO.Path]::GetFullPath($LogFile))) | |
[Void](Add-Member -InputObject $Object -Type NoteProperty -Name FileName -Value $([System.IO.Path]::GetFileName($LogFile))) | |
[Void](Add-Member -InputObject $Object -Type NoteProperty -Name LineNumber -Value $LineNumber) | |
[Void](Add-Member -InputObject $Object -Type NoteProperty -Name Line -Value $_) | |
if (($IndexOfDate -ge 0) -and ($IndexOfTime -ge 0)) | |
{ | |
$DateTime = [DateTime]::Parse($Column[$IndexOfDate] + " " + $Column[$IndexOfTime]) | |
if (-not $Utc) | |
{ | |
$DateTime = $DateTime.ToLocalTime() | |
} | |
[Void](Add-Member -InputObject $Object -Type NoteProperty -Name DateTime -Value $DateTime) | |
} | |
for ($Index = 0; $Index -lt $Field.Length; $Index++) | |
{ | |
[Void](Add-Member -InputObject $Object -Type NoteProperty -Name $Field[$Index] -Value $Column[$Index]) | |
} | |
$Object | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment