Get-Netstat 1,0 (modification of post by halr9000 view diff)
View followups from glnsize and glnsize | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/557"></script>download | new post
This will perform a basic netstat.exe command and “objectize” its output.
v0.9 Initial Build
V1.0 Added support for UDP, and processname
- $null, $null, $null, $null, $netstat = netstat -a -n -o
- [regex]$regexTCP = '(?<Protocol>\S+)\s+(?<LocalAddress>\S+)\s+(?<RemoteAddress>\S+)\s+(?<State>\S+)\s+(?<PID>\S+)'
- [regex]$regexUDP = '(?<Protocol>\S+)\s+(?<LocalAddress>\S+)\s+(?<RemoteAddress>\S+)\s+(?<PID>\S+)'
- foreach ($net in $netstat)
- {
- switch -regex ($net.Trim())
- {
- $regexTCP
- {
- $process = "" | Select-Object Protocol, LocalAddress, RemoteAddress, State, PID, ProcessName
- $process.Protocol = $matches.Protocol
- $process.LocalAddress = $matches.LocalAddress
- $process.RemoteAddress = $matches.RemoteAddress
- $process.State = $matches.State
- $process.PID = $matches.PID
- $process.ProcessName = ( Get-Process -Id $matches.PID ).ProcessName
- $process
- continue
- }
- $regexUDP
- {
- $process = "" | Select-Object Protocol, LocalAddress, RemoteAddress, State, PID, ProcessName
- $process.Protocol = $matches.Protocol
- $process.LocalAddress = $matches.LocalAddress
- $process.PID = $matches.PID
- $process.ProcessName = ( Get-Process -Id $matches.PID ).ProcessName
- $process
- continue
- }
- }
- }
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