Skip to content

Instantly share code, notes, and snippets.

@raspi
Created November 29, 2018 13:10
Show Gist options
  • Save raspi/8c6b416b7d5f698f69e51f6cf88205b3 to your computer and use it in GitHub Desktop.
Save raspi/8c6b416b7d5f698f69e51f6cf88205b3 to your computer and use it in GitHub Desktop.
List established IPs and ports with DNS resolved names and program names
# List established IPs and ports with DNS resolved names and program names
$nfo = @()
ForEach ($c in Get-NetTCPConnection -State Established) {
if ($c.LocalAddress -eq $c.RemoteAddress) {
# skip same IP's
Continue
}
$prog = (Get-Process -Id $c.OwningProcess)
try {
# Try to resolve IP to host name
$r = [System.Net.Dns]::GetHostEntry($c.RemoteAddress).HostName
} catch {
$r = "-"
}
$nfo += New-Object PSObject -Property @{
LocalAddress = $c.LocalAddress
LocalPort = $c.LocalPort
RemoteHost = $r
RemoteAddress = $c.RemoteAddress
RemotePort = $c.RemotePort
ProcName = $prog.Name
ProcPath = $prog.Path
ProcProduct = $prog.Product
PID = $c.OwningProcess
}
}
$nfo | Select -ErrorAction SilentlyContinue RemoteAddress, RemotePort, RemoteHost, LocalAddress, LocalPort, ProcProduct, PID, ProcPath, * | Sort PID,RemoteAddress,RemotePort | Format-Table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment