Created
March 5, 2019 05:58
-
-
Save dgosbell/b071acab69c006ce5d1a5f291b127e6b to your computer and use it in GitHub Desktop.
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 Get-NetworkStatistics | |
{ | |
$properties = ‘Protocol’,’LocalAddress’,’LocalPort’ | |
$properties += ‘RemoteAddress’,’RemotePort’,’State’,’ProcessName’,’PID’ | |
netstat -ano | Select-String -Pattern ‘\s+(TCP|UDP)’ | ForEach-Object { | |
$item = $_.line.split(” “,[System.StringSplitOptions]::RemoveEmptyEntries) | |
if($item[1] -notmatch ‘^\[::’) | |
{ | |
if (($la = $item[1] -as [ipaddress]).AddressFamily -eq ‘InterNetworkV6’) | |
{ | |
$localAddress = $la.IPAddressToString | |
$localPort = $item[1].split(‘\]:’)[-1] | |
} | |
else | |
{ | |
$localAddress = $item[1].split(‘:’)[0] | |
$localPort = $item[1].split(‘:’)[-1] | |
} | |
if (($ra = $item[2] -as [ipaddress]).AddressFamily -eq ‘InterNetworkV6’) | |
{ | |
$remoteAddress = $ra.IPAddressToString | |
$remotePort = $item[2].split(‘\]:’)[-1] | |
} | |
else | |
{ | |
$remoteAddress = $item[2].split(‘:’)[0] | |
$remotePort = $item[2].split(‘:’)[-1] | |
} | |
New-Object PSObject -Property @{ | |
PID = $item[-1] | |
ProcessName = (Get-Process -Id $item[-1] -ErrorAction SilentlyContinue).Name | |
Protocol = $item[0] | |
LocalAddress = $localAddress | |
LocalPort = $localPort | |
RemoteAddress =$remoteAddress | |
RemotePort = $remotePort | |
State = if($item[0] -eq ‘tcp’) {$item[3]} else {$null} | |
} | Select-Object -Property $properties | |
} | |
} | |
} | |
Get-NetworkStatistics | where processname -eq "msmdsrv" | format-table | |
Get-NetworkStatistics | where processname -eq "pbidesktop" | format-table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment