Show-HTNetworkConnections.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function Show-HTNetworkConnections {
    [CmdletBinding()]
    param (
        # Set this switch to monitor continuously
        [switch]$Continuous,

        # Refresh Rate (seconds)
        [Parameter()]
        [int16]$RefreshPerSecond = 1
    )
    process {
        $continue = $true
        While($continue)
        {

            $stat = [pscustomobject]@{ Established = 0; TimeWait = 0}

            netstat -ant | Set-Variable -Name nt

            $nt | findstr EST | Measure-Object -OutVariable est | Out-Null
            $nt | findstr TIME | Measure-Object -OutVariable tmwait | Out-Null
            Start-Sleep -Seconds $RefreshPerSecond
            $stat.Established = $est[0].Count
            $stat.TimeWait = $tmwait[0].Count
            Clear-Host
            $stat | Format-Table

            if(-not $Continuous) { $continue = $false}
        }
    }

}