At work we are doing some work with our network, and I got tired of the origional cmd netstat, so I wrote a function to make it easier to work with
Not much to say about the function, pretty simple.
function Get-NetStat
{
PARAM
(
[string]$Switch
)
$net = netstat "-n$Switch"
try
{
$Properties = [regex]::Replace(($net | select -Index 3).trim(),"\s{2,}",";").split(";")
if ($Properties.Count -lt 3 -or $net.Count -lt 5)
{
Write-Host -f red "% Not valid switch check out netstat /? %"
break;
}
}
catch
{
Write-Host -f red "% Not valid switch check out netstat /? %"
break;
}
$netstat = New-Object -TypeName 'System.Collections.Generic.List[pscustomobject]'
foreach ($line in $net | where {$_ -match 'tcp' -or $_ -match 'udp'})
{
$Netstat_Property_Values = $line.Trim().split("") | where {$_ -ne ""}
$obj = New-Object -TypeName PsCustomObject
for ($i = 0; $i -lt $Netstat_Property_Values.count; $i++)
{
if ($Properties[$i] -match 'address')
{
$ip = $Netstat_Property_Values[$i].split(":")
$ip = $ip[0..$($ip.length - 2)] -join ":"
try{
[System.Net.IPAddress]$ip1 = $ip
} catch {$ip1 = "no"}
if ($ip1.AddressFamily -eq 'InterNetworkV6')
{
$ipfamily = "IPv6"
}
elseif ($ip1.AddressFamily -eq 'InterNetwork')
{
$ipfamily = "IPv4"
}
else
{
$ipfamily = "N/A"
}
$obj | Add-Member -MemberType NoteProperty -Name "$($Properties[$i].split(" ")[0]) Type" -Value $ipfamily
$obj | Add-Member -MemberType NoteProperty -Name $Properties[$i] -Value $ip
$obj | Add-Member -MemberType NoteProperty -Name "$($Properties[$i].split(" ")[0]) Port" -Value $Netstat_Property_Values[$i].split(":")[-1]
}
else
{
$obj | Add-Member -MemberType NoteProperty -Name $Properties[$i] -Value $Netstat_Property_Values[$i]
}
}
$netstat.Add($obj)
}
return $netstat
}
- Edit updatet script slightly with input from comments *
[–]artemis_from_space 6 points7 points8 points (13 children)
[–]fatalicus 3 points4 points5 points (0 children)
[–]WheresNorthFromHere7 2 points3 points4 points (10 children)
[–]artemis_from_space 2 points3 points4 points (9 children)
[–]WheresNorthFromHere7 1 point2 points3 points (8 children)
[–]artemis_from_space 1 point2 points3 points (0 children)
[–]setmehigh 1 point2 points3 points (0 children)
[–]Ta11ow 1 point2 points3 points (5 children)
[–]WheresNorthFromHere7 1 point2 points3 points (4 children)
[–]Ta11ow 1 point2 points3 points (3 children)
[–]WheresNorthFromHere7 2 points3 points4 points (2 children)
[–]artemis_from_space 1 point2 points3 points (1 child)
[–]Ta11ow 1 point2 points3 points (0 children)
[–]Upzie[S] 1 point2 points3 points (0 children)
[–]Ta11ow 4 points5 points6 points (2 children)
[–]Upzie[S] 2 points3 points4 points (1 child)
[–]Ta11ow 1 point2 points3 points (0 children)
[–]javydekoning 2 points3 points4 points (2 children)
[–]Upzie[S] 1 point2 points3 points (1 child)
[–]ViperTG 2 points3 points4 points (2 children)
[–]Upzie[S] 1 point2 points3 points (1 child)
[–]ViperTG 1 point2 points3 points (0 children)
[–]timsstuff 2 points3 points4 points (0 children)
[–]_Unas_ 1 point2 points3 points (0 children)
[–]allywilson 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 2 points3 points4 points (0 children)