Get-IISLog.ps1
<#PSScriptInfo .VERSION 1.0.4 .GUID 8bc54682-a5f5-4fa9-9fc6-37a35105b4e5 .AUTHOR saw-friendship .COMPANYNAME .COPYRIGHT .TAGS IIS Log .LICENSEURI .PROJECTURI https://sawfriendship.wordpress.com .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .DESCRIPTION Get IIS Log content as objects .EXAMPLE Get-IISLog -Path 'C:\inetpub\logs\LogFiles\W3SVC4\u_extend1.log' -Tail 10 -Wait .EXAMPLE ls C:\inetpub\logs\LogFiles\W3SVC4\*.log | Get-IISLog -Tail 10 -IncludeLogFilePath #> param( [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][alias('PSPath','FullName')][string[]]$Path, [int]$ReadCount = 100, [int]$Tail, [switch]$Wait, [switch]$IncludeLogFilePath ) Begin { $ContentParam = @{ 'ReadCount' = [int]$ReadCount 'Wait' = [bool]$Wait } if(($MyInvocation.BoundParameters.ContainsKey('Tail')) -and ($PSVersionTable.PSVersion.Major -ge 3)){ $ContentParam.Tail = [int]$Tail } } Process { $Path | % { $LogFilePath = ($_ | Get-Item).FullName [string[]]$Fields = @() [string[]]$Fields = (@(Get-Content -Path $_ -TotalCount 10 | Select-String -Pattern '^#Fields:\s+') | Select-Object -First 1) -replace '^#Fields:\s+' -split '\s+' if ($Fields.Count -ge 1) { Get-Content @ContentParam -Path $_ | % { $_ | % { $String = $_ $StringArray = @($_ -split "\s+") if ($String -match '^#') { if ($String -match '^#Fields:\s+') { [string[]]$Fields = $String -replace '^#Fields:\s+' -split "\s+" } } else { $Fields | % -Begin { $i = 0 $Hash = @{} } -Process { $Hash.Add($Fields[$i], $StringArray[$i]) $i = $i + 1 } -End { if(! [bool]$IncludeLogFilePath){ New-Object -TypeName PSCustomObject -Property $Hash | Select-Object -Property $Fields } else { New-Object -TypeName PSCustomObject -Property $Hash | Select-Object -Property $Fields | Add-Member -MemberType NoteProperty -Name LogFilePath -Value $LogFilePath -Force -PassThru } } } } } } } } End {} |