Get-Netstat 0.9 (modification of post by halr9000 view diff)
View followups from glnsize | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/555"></script>download | new post
This will perform a basic netstat.exe command and “objectize” its output. This version only handles the TCP stuff because I ran out of time. This needs perhaps a switch statement and a second regex to do the rest justice.
I’d like to see in a v2 the capability to tie process names in via the PID column and output the real process name and file info and so on.
- $null, $null, $null, $null, $netstat = netstat -a -n -o
- [regex]$regex = '\s+(?<Protocol>\S+)\s+(?<LocalAddress>\S+)\s+(?<RemoteAddress>\S+)\s+(?<State>\S+)\s+(?<PID>\S+)'
- $netstat | ForEach-Object {
- if ( $_ -match $regex ) {
- $process = "" | Select-Object Protocol, LocalAddress, RemoteAddress,
- State, PID
- $process.Protocol = $matches.Protocol
- $process.LocalAddress = $matches.LocalAddress
- $process.RemoteAddress = $matches.RemoteAddress
- $process.State = $matches.State
- $process.PID = $matches.PID
- $process
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository