Created
May 30, 2013 00:35
-
-
Save cdhunt/5675003 to your computer and use it in GitHub Desktop.
Second attempt at Event 5 based on what I learned from feedback and judging other scripts.
This file contains hidden or 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
function Select-UniqueClientIP { | |
[CmdletBinding(SupportsPaging,DefaultParameterSetName="IPv4")] | |
[OutputType([System.String[]])] | |
param( | |
[Parameter(Position=0,Mandatory,ValueFromPipeline,ValueFromPipeLineByPropertyName)] | |
[ValidateNotNullOrEmpty()] | |
[Alias("FullName")] | |
[string[]] | |
$Path, | |
[Parameter(Position=1)] | |
[ValidateSet("W3C", "IIS", "NCSA")] | |
[Alias("Type")] | |
[string] | |
$Format = "W3C", | |
[Parameter(Position=1)] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$Filter, | |
[Parameter(ParameterSetName="IPv4")] | |
[Alias("v4")] | |
[Switch] | |
$Ipv4, | |
[Parameter(ParameterSetName="IPv6")] | |
[Alias("v6")] | |
[Switch] | |
$Ipv6 | |
) | |
Begin | |
{ | |
$_accumulator = @() | |
[scriptblock]$VersionFilter = {$_} | |
if ($Ipv4) | |
{ | |
$VersionFilter = {$PSItem.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork} | |
} | |
if ($Ipv6) | |
{ | |
$VersionFilter = {$PSItem.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6} | |
} | |
switch ($Format) { | |
"IIS" { | |
$ClientIPIndex = 0 | |
$Delimiter = ',' | |
break | |
} | |
"NCSA" { | |
$ClientIPIndex = 0 | |
$Delimiter = ' ' | |
break | |
} | |
default { | |
$ClientIPIndex = -1 | |
$Delimiter = ' ' | |
break | |
} | |
} | |
} | |
Process{ | |
$fileCounter = 0 | |
Foreach ($file in $Path) | |
{ | |
$fileCounter++ | |
try { | |
$stream = [IO.StreamReader]$file | |
} | |
catch | |
{ | |
Write-Warning -Message "Could not open $Path. $($_.Exception.Message)" | |
} | |
$lineCounter = 0 | |
do | |
{ | |
$lineCounter++ | |
if ($Format -eq "W3C") | |
{ | |
if ([char]$stream.Peek() -eq '#') | |
{ | |
$ClientIPIndex = -1 | |
} | |
if ($ClientIPIndex -lt 0) | |
{ | |
$line = $stream.ReadLine() | |
while ($line -notmatch "^#field") | |
{ | |
$line = $stream.ReadLine() | |
} | |
$ClientIPIndex = [Array]::IndexOf($line.Split($Delimiter), 'c-ip') - 1 | |
} | |
} | |
$line = $stream.ReadLine() | |
try | |
{ | |
$_accumulator += [Net.IPAddress]$line.Split($Delimiter)[$ClientIPIndex] | |
} | |
catch | |
{ | |
Write-Verbose -Message "Could not interpret IP on line $lineCounter of $file. $_.Exception.Message" | |
} | |
} while (-not $stream.EndOfStream) | |
} | |
} | |
End | |
{ | |
#Send result to pipeline | |
$selectParams = @{Property = @("Count", @{Name="IPAddress";Expression={$PSitem.Name}})} | |
if ($PSBoundParameters['Skip']) | |
{ | |
$selectParams['Skip'] = $PSCmdlet.PagingParameters.Skip | |
} | |
if ($PSBoundParameters['First']) | |
{ | |
$selectParams['First'] = $PSCmdlet.PagingParameters.First | |
} | |
$uniqueResults = $_accumulator | | |
Where-Object -FilterScript $VersionFilter | | |
Group-Object -Property IPAddressToString | |
$uniqueResults | Select-Object @selectParams | Write-Output | |
$count = 0 | |
if ($PSCmdlet.PagingParameters.IncludeTotalCount) | |
{ | |
$TotalCountAccuracy = 1.0 | |
if ($uniqueResults) | |
{ | |
$count = $uniqueResults.Count | |
} | |
$TotalCount = $PSCmdlet.PagingParameters.NewTotalCount($count, $TotalCountAccuracy) | |
Write-Output $TotalCount | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment