Skip to content

Instantly share code, notes, and snippets.

@tylerapplebaum
Last active March 18, 2019 20:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerapplebaum/f6577e0fa53f095f1ad64ca7e684db5a to your computer and use it in GitHub Desktop.
Save tylerapplebaum/f6577e0fa53f095f1ad64ca7e684db5a to your computer and use it in GitHub Desktop.
Just like "netstat -f | findstr ESTABLISHED" but in PowerShell
Function Resolve-EstablishedConnections {
$EstConnections = Get-NetTCPConnection -State Established
$ConnectionArr = New-Object System.Collections.ArrayList #Initialize the ArrayList
$Counter = 1
ForEach ($Connection in $EstConnections) {
Write-Progress -Activity "Resolving PTR Record" -Status "Looking up $($Connection.RemoteAddress)" -PercentComplete ($Counter / $($EstConnections.Length)*100)
$ConnectionObj = [PSCustomObject][Ordered] @{
LocalAddress = $Connection.LocalAddress
LocalPort = $Connection.LocalPort
RemoteAddress = $Connection.RemoteAddress
RemoteName = Resolve-DnsName -Name $Connection.RemoteAddress -ErrorAction SilentlyContinue | Select-Object -ExpandProperty NameHost
RemotePort = $Connection.RemotePort
State = $Connection.State
}
$ConnectionArr.Add($ConnectionObj) | Out-Null #Add the object to the array
$Counter++
} #End ForEach
$ConnectionArr | Out-GridView -Title Resolve-EstablishedConnections
Return $ConnectionArr
} #End Resolve-EstablishedConnections
. Resolve-EstablishedConnections
@tylerapplebaum
Copy link
Author

Output:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment