首先以管理管权限打开powershell
Set-ExecutionPolicy RemoteSigned
然后运行脚本
如果你是vip,可以在文章上方直接下载
如果不是,可以复制下面的脚本自己新建文件tcpcounts.ps1
$netstatOutput = netstat -n | findstr /C:"TCP"
$tasklistOutput = tasklist
# 正则表达式匹配正在运行的应用程序的名称和PID
$processes = $tasklistOutput | Select-String -Pattern '(\S+)\s+(\d+)' | ForEach-Object { [PSCustomObject]@{ Name = $_.Matches[1].Value; PID = $_.Matches[2].Value } }
$connections = Get-NetTCPConnection | Where-Object { $_.State -eq 'Established' }
# 遍历TCP连接并统计每个应用程序的连接数量
$connectionCount = @{}
foreach ($connection in $connections) {
$process = Get-Process -Id $connection.OwningProcess
$appName = $process.ProcessName
if ($connectionCount.ContainsKey($appName)) {
$connectionCount[$appName]++
} else {
$connectionCount[$appName] = 1
}
}
# 显示每个应用程序的TCP连接数量
$connectionCount.GetEnumerator() | Sort-Object Name | Format-Table -AutoSize
就可以看到