PowerShell MyNetStat

11 views
Skip to first unread message

alex;

unread,
Sep 2, 2025, 7:04:05 PM (3 days ago) Sep 2
to xbasians.org
I want to share this # Интервал автообновления в секундах
$interval = 5
$lastUpdate = Get-Date
$filter = "All"  # "All", "Established" или "Other"

function Show-Connections {
    $list = @()
    $conns = Get-NetTCPConnection

    foreach ($c in $conns) {
        try {
            $proc = Get-Process -Id $c.OwningProcess -ErrorAction Stop

            # Определяем направление
            $direction = switch ($c.State) {
                "Bound"   { "Listening" }
                "Listen"  { "Listening" }
                "Established" {
                    if ($c.LocalPort -lt 1024) { "Inbound" } else { "Outbound" }
                }
                default { $c.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" { "Все соединения" }
        "Established" { "Только Established" }
        "Other" { "Другие соединения" }
    }
    Write-Host "=== $title ==="
    $list | Sort-Object ProcessName, PID | Format-Table -AutoSize
    Write-Host "`n[Space] обновить, [E] только Established, [O] остальные, [A] все, [Esc] выйти, автообновление каждые $interval сек."
}

# Первый вывод
Show-Connections

do {
    # Автообновление по интервалу
    if ((Get-Date) - $lastUpdate -gt (New-TimeSpan -Seconds $interval)) {
        Show-Connections
        $lastUpdate = Get-Date
    }

    # Проверяем клавиши без блокировки
    if ([Console]::KeyAvailable) {
        $key = [Console]::ReadKey($true).Key
        switch ($key) {
            "Escape" { break }
            "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 }
        }
    }

    Start-Sleep -Milliseconds 100
} while ($true)

alex;

unread,
Sep 4, 2025, 3:16:21 AM (yesterday) Sep 4
to xbasians.org
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;:
Reply all
Reply to author
Forward
0 new messages