Advertisement
upz

Get-NetStat

upz
Feb 28th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-NetStat
  2. {
  3. PARAM
  4. (
  5.     [validateset(
  6.         "Active"
  7.     ,   "All"
  8.     )]
  9.     [string]$state = "Active"
  10. )
  11.     $net = netstat "-no"
  12.     $Properties = [regex]::Replace(($net | select -Index 3).trim(),"\s{2,}",";").split(";")
  13.    
  14.     $netstat =  New-Object -TypeName 'System.Collections.Generic.List[pscustomobject]'
  15.     foreach ($line in $net | where {$_ -match 'tcp' -or $_ -match 'udp'})
  16.     {
  17.         $Netstat_Property_Values = $line.Trim().split("") | where {$_ -ne ""}
  18.  
  19.         $obj = New-Object -TypeName PsCustomObject
  20.  
  21.         for ($i = 0; $i -lt $Netstat_Property_Values.count; $i++)
  22.         {
  23.             if ($Properties[$i] -match 'address')
  24.             {
  25.                 $ip = $Netstat_Property_Values[$i].split(":")
  26.                 $ip = $ip[0..$($ip.length - 2)] -join ":"
  27.                 try{
  28.                     [System.Net.IPAddress]$ip1 = $ip
  29.                 } catch {$ip1 = "no"}
  30.  
  31.                 if ($ip1.AddressFamily -eq 'InterNetworkV6')
  32.                 {
  33.                     $ipfamily = "IPv6"
  34.                 }
  35.                 elseif ($ip1.AddressFamily -eq 'InterNetwork')
  36.                 {
  37.                     $ipfamily = "IPv4"
  38.                 }
  39.                 else
  40.                 {
  41.                     $ipfamily = "N/A"
  42.                 }
  43.                
  44.                 [int]$Port = $Netstat_Property_Values[$i].split(":")[-1]
  45.                 [ipaddress]$ipa = $ip
  46.  
  47.                 $obj | Add-Member -MemberType NoteProperty -Name $Properties[$i] -Value $ipa
  48.                 $obj | Add-Member -MemberType NoteProperty -Name "$($Properties[$i].split(" ")[0][0])Type" -Value $ipfamily
  49.                 $obj | Add-Member -MemberType NoteProperty -Name "$($Properties[$i].split(" ")[0][0])Port" -Value $port
  50.             }
  51.             elseif ($Properties[$i] -match 'pid')
  52.             {
  53.                 $obj | Add-Member -MemberType NoteProperty -Name $Properties[$i] -Value $Netstat_Property_Values[$i]
  54.                 $obj | Add-Member -MemberType NoteProperty -Name "Process" -Value "$((get-process | where {$_.Id -eq $obj.PID}).Name)"
  55.             }
  56.             else
  57.             {
  58.                  $obj | Add-Member -MemberType NoteProperty -Name $Properties[$i] -Value $Netstat_Property_Values[$i]
  59.             }
  60.         }        
  61.  
  62.         $netstat.Add($obj)
  63.     }
  64.  
  65.     switch ($state)
  66.     {
  67.         "All"
  68.         {
  69.             return $netstat
  70.             break;
  71.         }
  72.  
  73.         "Active"
  74.         {
  75.             return ($netstat | where {$_.pid -ne 0})
  76.             break;
  77.         }
  78.     }
  79.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement