TurboPing.psm1

#New-Alias -Name TP -Value TurboPing -Scope Global
$Param = ""

Function IPRange ($NetID, $Pref) {

$inv = (32 - $Pref)
$ipb = [Net.IPAddress]::Parse($NetID).GetAddressBytes()
[Array]::Reverse($ipb)
$ip = [BitConverter]::ToInt32($ipb, 0)
    For ($i = 1; $i -lt ((1 -shl $inv) - 1); $i++) {
        $ipi = ($ip -bor $i)
        $ipib = [BitConverter]::GetBytes($ipi)
        [Array]::Reverse($ipib)
        New-Object IPAddress(@(,$ipib))
    }
}

Workflow Ping ($IPResult, $Ports) {
    foreach -parallel ($IP in $IPResult.IP) {
        InlineScript {
            $IP = $Using:IP
            $Ports = $Using:Ports
            if (Test-NetConnection $IP -InformationLevel Quiet -WarningAction SilentlyContinue -ErrorAction SilentlyContinue) {
                [Array]$PortsResult += "ICMP"
            }
            if ($Ports) {
                ForEach ($Port in $Ports) {
                    if (Test-NetConnection $IP -Port $Port -InformationLevel Quiet -WarningAction SilentlyContinue -ErrorAction SilentlyContinue) {
                    [Array]$PortsResult += $Port
                    }
                }
            }
            New-Object PSObject -Property @{'Result'=$PortsResult; 'IP'="$IP"; }            
        }
    }
}

#Invoke-WebRequest -Uri http://standards.ieee.org/develop/regauth/oui/oui.txt -OutFile "$env:TEMP\oui.txt"
#(Get-Content "$env:TEMP\oui.txt" | ? {$_ -like "*04-92-26*"}).Split("`t")[2]

Function FindIP ($array, $value) {
    for ($i=0; $i -lt $array.count;$i++) {
        if($array[$i] -eq $value){$i}
    }
}

Function IPResult {
    $k=0
    $n=([math]::Floor($IPResult.Count/5))
    while ($k -le $n) {
        ping $IPResult[($k*5)..($k*5+4)] $Ports | ForEach {
        $i = (FindIP $IPResult.IP $_.IP)
        $IPResult[$i] | Add-Member NoteProperty Result $_.Result -Force
   
        } 
        $IPResult[($k*5)..($k*5+4)] | ForEach {if ($_.Result) {
            Write-Host $_.IP $_.Result -ForegroundColor Green -Separator " `t"
        }
        else {
            Write-Host $_.IP "FAIL" -ForegroundColor Red -Separator " `t"
        }}
        $k++
    }
}

Function TurboPing ([string]$Param) {
    If (!$Param) {
        $NIC = (Get-NetIPConfiguration | ? {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne "Disconnected"})
        $IPLoc = [IPaddress]($NIC.IPv4Address.IPAddress)
        $PrefLoc = $NIC.IPv4Address.PrefixLength
        $Bytes = [BitConverter]::GetBytes([UInt32](([Math]::Pow(2, $PrefLoc) - 1) * [Math]::Pow(2, (32 - $PrefLoc))))
        $MaskLoc = [IPaddress]((($Bytes.Count - 1).. 0 | ForEach-Object {[String] $Bytes[$_] }) -join ".")
        $NetIDLoc = [IPaddress] ($IPLoc.Address -band $MaskLoc.Address)
        $IPRange = (IPRange $NetIDLoc.IPAddressToString $PrefLoc).IPAddressToString
        $IPResult = $IPRange | ForEach-Object {New-Object PSObject -Property @{IP="$_"}}
        $Ports = @(22)
        IPResult
    }
    ElseIf ($Param.IndexOf(":") -eq -1) {
        Write-Host "Only Address"
    }
    ElseIf ($Param.IndexOf(":") -eq 0) {
        Write-Host "Only Ports"
    }
    ElseIf ($Param.IndexOf(":") -gt 0) {
        Write-Host "Adress and Port"
    }
}

TurboPing $Param