fixed
# Auto-refresh interval in seconds
$interval = 5
$lastUpdate = Get-Date
$filter = "All" # "All", "Established" or "Other"
$exit = $false
function Show-Connections {
$list = @()
$conns = Get-NetTCPConnection
foreach ($c in $conns) {
try {
$proc = Get-Process -Id $c.OwningProcess -ErrorAction Stop
# Determine direction
$direction = switch ($c.State) {
"Bound" { "Listening" }
"Listen" { "Listening" }
"Established" {
if ($c.LocalPort -lt 1024) { "Inbound" } else { "Outbound" }
}
default { $c.State }
}
# Filter by state
if ($filter -eq "Established" -and $c.State -ne "Established") { continue }
if ($filter -eq "Other" -and $c.State -eq "Established") { continue }
$list += [PSCustomObject]@{
ProcessName = $proc.ProcessName
PID = $c.OwningProcess
Path = $proc.Path
State = $c.State
Direction = $direction
Local = "$($c.LocalAddress):$($c.LocalPort)"
Remote = "$($c.RemoteAddress):$($c.RemotePort)"
}
} catch {}
}
Clear-Host
$title = switch ($filter) {
"All" { "All connections" }
"Established" { "Only Established" }
"Other" { "Other connections" }
}
Write-Host "=== $title ==="
$list | Sort-Object ProcessName, PID | Format-Table -AutoSize
Write-Host "`n[Space] refresh, [E] only Established, [O] others, [A] all, [Esc] exit, auto-refresh every $interval sec."
}
# First output
Show-Connections
do {
# Auto-refresh by interval
if ((Get-Date) - $lastUpdate -gt (New-TimeSpan -Seconds $interval)) {
Show-Connections
$lastUpdate = Get-Date
}
# Check keys without blocking
if ([Console]::KeyAvailable) {
$key = [Console]::ReadKey($true).Key
switch ($key) {
"Escape" { $exit = $true }
"Spacebar" { Show-Connections; $lastUpdate = Get-Date }
"E" { $filter = "Established"; Show-Connections; $lastUpdate = Get-Date }
"O" { $filter = "Other"; Show-Connections; $lastUpdate = Get-Date }
"A" { $filter = "All"; Show-Connections; $lastUpdate = Get-Date }
}
}
if ($exit -eq $true) { break }
Start-Sleep -Milliseconds 100
} while ($true)
среда, 3 сентября 2025 г. в 02:04:05 UTC+3, alex;: