PoshCode Logo PowerShell Code Repository

Difference between
modified post 3118 by JAMES RU8SSELL on Fri 23rd Dec 13:43 and
original post 2574 by Mark Shevchenko on Sun 20th Mar 07:05
Showold version | new version | both versions

    
11
param
22
(
33
	[Parameter(
44
		Mandatory=$true,
55
		Position = 0,
66
		ValueFromPipeline=$true,
77
		HelpMessage="Specifies the path to the IIS *.log file to import. You can also pipe a path to Import-Iss-Log."
88
	)]
99
	[ValidateNotNullOrEmpty()]
1010
	[string]
1111
	$Path,
1313
	[Parameter(
1414
		Position = 1,
1515
		HelpMessage="Specifies the delimiter that separates the property values in the IIS *.log file. The default is a spacebar."
1616
	)]
1717
	[ValidateNotNullOrEmpty()]
1818
	[string]
1919
	$Delimiter = " ",
2121
	[Parameter(HelpMessage="The character encoding for the IIS *log file. The default is the UTF8.")]
2222
	[Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]
2323
	$Encoding = [Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]::UTF8
2424
)
2626
begin
2727
{
2828
	$fieldNames = @()
3030
	$output = New-Object Object
3131
	Add-Member -InputObject $output -MemberType NoteProperty -Name "DateTime" -Value $null
3232
	Add-Member -InputObject $output -MemberType NoteProperty -Name "ClientHost" -Value $null
3333
	Add-Member -InputObject $output -MemberType NoteProperty -Name "UserName" -Value $null
3434
	Add-Member -InputObject $output -MemberType NoteProperty -Name "Service" -Value $null
3535
	Add-Member -InputObject $output -MemberType NoteProperty -Name "Machine" -Value $null
3636
	Add-Member -InputObject $output -MemberType NoteProperty -Name "ServerIp" -Value $null
3737
	Add-Member -InputObject $output -MemberType NoteProperty -Name "ServerPort" -Value $null
3838
	Add-Member -InputObject $output -MemberType NoteProperty -Name "Method" -Value $null
3939
	Add-Member -InputObject $output -MemberType NoteProperty -Name "ScriptPath" -Value $null
4040
	Add-Member -InputObject $output -MemberType NoteProperty -Name "QueryString" -Value $null
4141
	Add-Member -InputObject $output -MemberType NoteProperty -Name "ServiceStatus" -Value $null
4242
	Add-Member -InputObject $output -MemberType NoteProperty -Name "ServiceSubStatus" -Value $null
4343
	Add-Member -InputObject $output -MemberType NoteProperty -Name "Win32Status" -Value $null
4444
	Add-Member -InputObject $output -MemberType NoteProperty -Name "BytesSent" -Value $null
4545
	Add-Member -InputObject $output -MemberType NoteProperty -Name "BytesRecived" -Value $null
4646
	Add-Member -InputObject $output -MemberType NoteProperty -Name "ProcessingTime" -Value $null
4747
	Add-Member -InputObject $output -MemberType NoteProperty -Name "ProtocolVersion" -Value $null
4848
	Add-Member -InputObject $output -MemberType NoteProperty -Name "Host" -Value $null
4949
	Add-Member -InputObject $output -MemberType NoteProperty -Name "UserAgent" -Value $null
5050
	Add-Member -InputObject $output -MemberType NoteProperty -Name "Cookie" -Value $null
5151
	Add-Member -InputObject $output -MemberType NoteProperty -Name "Referer" -Value $null
5252
}
5454
process
5555
{
5656
	foreach($line in Get-Content -Path $Path -Encoding $Encoding)
5757
	{
5858
		if($line.StartsWith("#Fields: "))
5959
		{
6060
			$fieldNames = @($line.Substring("#Fields: ".Length).Split($Delimiter));
6161
		}
6262
		elseif(-not $line.StartsWith("#"))
6363
		{
6464
			$fieldValues = @($line.Split($Delimiter));
6666
			for($i = 0; $i -lt $fieldValues.Length; $i++)
6767
			{
6868
				$name = $fieldNames[$i]
6969
				$value = $fieldValues[$i]
7171
				switch($name)
7272
				{
7373
				"date" { $output.DateTime = [DateTime]::Parse($value) }
7474
				"time" { $output.DateTime += [TimeSpan]::Parse($value) }
7575
				"c-ip" { $output.ClientHost = [System.Net.IPAddress]::Parse($value) }
7676
				"cs-username" { $output.UserName = if($value -eq '-') { $null } else { $value } }
7777
				"s-sitename" { $output.Service = $value }
7878
				"s-computername" { $output.Machine = $value }
7979
				"s-ip" { $output.ServerIp = [System.Net.IPAddress]::Parse($value) }
8080
				"s-port" { $output.ServerPort = [int]$value }
8181
				"cs-method" { $output.Method = $value }
8282
				"cs-uri-stem" { $output.ScriptPath = [System.Web.HttpUtility]::UrlDecode($value) }
8383
				"cs-uri-query" { $output.QueryString = if($value -eq '-') { $null } else { [System.Web.HttpUtility]::UrlDecode($value) } }
8484
				"sc-status" { $output.ServiceStatus = [int]$value }
8585
				"sc-substatus" { $output.ServiceSubStatus = [int]$value }
8686
				"sc-win32-status" { $output.Win32Status = [BitConverter]::ToInt32([BitConverter]::GetBytes([UInt32]($value)), 0) }
8787
				"sc-bytes" { $output.BytesSent = [UInt64]$value }
8888
				"cs-bytes" { $output.BytesRecived = [UInt64]$value }
8989
				"time-taken" { $output.ProcessingTime = [int]$value }
9090
				"cs-version" { $output.ProtocolVersion = $value }
9191
				"cs-host" { $output.Host = if($value -eq '-') { $null } else { $value } }
9292
				"cs(User-Agent)" { $output.UserAgent = if($value -eq '-') { $null } else { $value } }
9393
				"cs(Cookie)" { $output.Cookie = if($value -eq '-') { $null } else { $value } }
9494
				"cs(Referer)" { $output.Referer = if($value -eq '-') { $null } else { [System.Web.HttpUtility]::UrlDecode($value) } }
9595
				}
9696
			}
9898
			Write-Output $output
9999
		}
100100
	}
101101
}

ContributeMost Recent Contributions (feed)

Contribute ... Next Page