-
-
Save shakahl/ea028567ec10595cc62254378c434d50 to your computer and use it in GitHub Desktop.
Example for parsing the output from netstat
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 parseNetstat { | |
| param([object[]]$Lines) | |
| if ($IsWindows) { | |
| $skip = 4 | |
| } else { | |
| $skip = 3 | |
| } | |
| $Lines | Select-Object -Skip $skip | ForEach-Object { | |
| $columns = ($_ -split ' ').Trim() | Where-Object {$_ } | |
| if ($columns[1].IndexOf('[') -lt 0) { | |
| $laddr = $columns[1].Split(':')[0] | |
| $lport = $columns[1].Split(':')[1] | |
| } else { | |
| $laddr = $columns[1].Split(']:')[0].Trim('[') | |
| $lport = $columns[1].Split(']:')[1] | |
| } | |
| if ($columns[2].IndexOf('[') -lt 0) { | |
| $raddr = $columns[2].Split(':')[0] | |
| $rport = $columns[2].Split(':')[1] | |
| } else { | |
| $raddr = $columns[2].Split(']:')[0].Trim('[') | |
| $rport = $columns[2].Split(']:')[1] | |
| } | |
| [pscustomobject]@{ | |
| Protocol = $columns[0] | |
| LocalAddress = $laddr | |
| LocalPort = $lport | |
| RemoteAddress = $raddr | |
| RemotePort = $rport | |
| State = $columns[3] | |
| } | |
| } | |
| } | |
| parseNetstat -Lines (netstat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment