创建 PowerShell 脚本,19点到23点每半小时执行1次,统计网卡连接数
建议采用 PowerShell + BAT + 计划任务(Task Scheduler) 的方式,因为你列出的命令都是 PowerShell Cmdlet,直接写到 BAT 中会比较麻烦。
1. 创建 PowerShell 脚本
保存为:
D:\temp\get_connection.ps1
内容如下:
$LogFile = "D:\temp\get_connection.log"
Add-Content $LogFile ""
Add-Content $LogFile "============================================================"
Add-Content $LogFile "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
Add-Content $LogFile "============================================================"
Add-Content $LogFile ""
Add-Content $LogFile "[Get-NetTCPConnection | Measure-Object]"
Get-NetTCPConnection | Measure-Object | Out-String | Add-Content $LogFile
Add-Content $LogFile ""
Add-Content $LogFile "[Get-NetAdapterVmq]"
Get-NetAdapterVmq | Format-Table -AutoSize | Out-String | Add-Content $LogFile
Add-Content $LogFile ""
Add-Content $LogFile "[Get-NetAdapterStatistics]"
Get-NetAdapterStatistics | Format-Table -AutoSize | Out-String | Add-Content $LogFile
Add-Content $LogFile ""
Add-Content $LogFile "[Get-NetTCPConnection | Group-Object State]"
Get-NetTCPConnection | Group-Object State | Format-Table -AutoSize | Out-String | Add-Content $LogFile
Add-Content $LogFile ""
Add-Content $LogFile "[Network Interface Bytes Total/sec]"
Get-Counter '\Network Interface(*)\Bytes Total/sec' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
Add-Content $LogFile ""
Add-Content $LogFile "[Hyper-V Virtual Switch Bytes/sec]"
Get-Counter '\Hyper-V Virtual Switch(*)\Bytes/sec' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
Add-Content $LogFile ""
Add-Content $LogFile "[Network Interface Packets Sent/sec]"
Get-Counter '\Network Interface(*)\Packets Sent/sec' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
Add-Content $LogFile ""
Add-Content $LogFile "[Hyper-V Virtual Network Adapter Bytes/sec]"
Get-Counter '\Hyper-V Virtual Network Adapter(*)\Bytes/sec' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
2. 创建 BAT 启动脚本
保存为:
D:\temp\get_connection.bat
内容:
@echo off
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "D:\temp\get_connection.ps1"
测试:
D:\temp\get_connection.bat
查看:
D:\temp\get_connection.log
是否生成日志。
3. 创建计划任务
打开:
任务计划程序
(Task Scheduler)
创建任务:
常规
名称:
Get_Connection_Monitor
使用最高权限运行
无论用户是否登录都运行
触发器
新增 8 个触发器:
| 时间 |
|---|
| 19:30 |
| 20:00 |
| 20:30 |
| 21:00 |
| 21:30 |
| 22:00 |
| 22:30 |
| 23:00 |
操作:
启动程序
程序:
D:\temp\get_connection.bat
4. PowerShell 一键创建计划任务(推荐)
管理员 PowerShell 执行:
$action = New-ScheduledTaskAction `
-Execute "D:\temp\get_connection.bat"
$times = @(
"19:30",
"20:00",
"20:30",
"21:00",
"21:30",
"22:00",
"22:30",
"23:00"
)
foreach ($t in $times)
{
$trigger = New-ScheduledTaskTrigger `
-Daily `
-At $t
Register-ScheduledTask `
-TaskName ("GetConnection_" + $t.Replace(":","")) `
-Action $action `
-Trigger $trigger `
-RunLevel Highest `
-Force
}
5. 建议增加 TCP 关键指标
为了分析你当前 Hyper-V + NAT + 70万连接场景,建议额外记录:
netstat -ano | find /c "ESTABLISHED"
Get-NetTCPSetting
Get-NetOffloadGlobalSetting
Get-NetAdapterRss
Get-NetAdapterVmq
Get-VMNetworkAdapter -ManagementOS
添加后的完整D:\temp\get_connection.ps1文件内容:
$LogFile = "D:\temp\get_connection.log"
Add-Content $LogFile ""
Add-Content $LogFile "===================================================================="
Add-Content $LogFile "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
Add-Content $LogFile "===================================================================="
# TCP连接总数
Add-Content $LogFile ""
Add-Content $LogFile "[Get-NetTCPConnection | Measure-Object]"
Get-NetTCPConnection | Measure-Object | Out-String | Add-Content $LogFile
# TCP状态统计
Add-Content $LogFile ""
Add-Content $LogFile "[Get-NetTCPConnection | Group-Object State]"
Get-NetTCPConnection | Group-Object State |
Sort-Object Count -Descending |
Format-Table Count,Name -AutoSize |
Out-String | Add-Content $LogFile
# ESTABLISHED数量
Add-Content $LogFile ""
Add-Content $LogFile "[ESTABLISHED Count]"
(netstat -ano | findstr ESTABLISHED | Measure-Object -Line).Lines |
Out-String | Add-Content $LogFile
# VMQ状态
Add-Content $LogFile ""
Add-Content $LogFile "[Get-NetAdapterVmq]"
Get-NetAdapterVmq |
Format-Table -AutoSize |
Out-String | Add-Content $LogFile
# RSS状态
Add-Content $LogFile ""
Add-Content $LogFile "[Get-NetAdapterRss]"
Get-NetAdapterRss |
Format-Table -AutoSize |
Out-String | Add-Content $LogFile
# 网卡统计
Add-Content $LogFile ""
Add-Content $LogFile "[Get-NetAdapterStatistics]"
Get-NetAdapterStatistics |
Format-Table -AutoSize |
Out-String | Add-Content $LogFile
# TCP参数
Add-Content $LogFile ""
Add-Content $LogFile "[Get-NetTCPSetting]"
Get-NetTCPSetting |
Format-Table -AutoSize |
Out-String | Add-Content $LogFile
# 网卡卸载状态
Add-Content $LogFile ""
Add-Content $LogFile "[Get-NetOffloadGlobalSetting]"
Get-NetOffloadGlobalSetting |
Format-List |
Out-String | Add-Content $LogFile
# Hyper-V虚拟网卡
Add-Content $LogFile ""
Add-Content $LogFile "[Get-VMNetworkAdapter -ManagementOS]"
try {
Get-VMNetworkAdapter -ManagementOS |
Format-Table -AutoSize |
Out-String | Add-Content $LogFile
}
catch {
Add-Content $LogFile "Hyper-V module not available."
}
# 网络流量
Add-Content $LogFile ""
Add-Content $LogFile "[Network Interface Bytes Total/sec]"
Get-Counter '\Network Interface(*)\Bytes Total/sec' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
# 网卡PPS
Add-Content $LogFile ""
Add-Content $LogFile "[Network Interface Packets Sent/sec]"
Get-Counter '\Network Interface(*)\Packets Sent/sec' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
Add-Content $LogFile ""
Add-Content $LogFile "[Network Interface Packets Received/sec]"
Get-Counter '\Network Interface(*)\Packets Received/sec' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
# Hyper-V Virtual Switch流量
Add-Content $LogFile ""
Add-Content $LogFile "[Hyper-V Virtual Switch Bytes/sec]"
Get-Counter '\Hyper-V Virtual Switch(*)\Bytes/sec' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
# Hyper-V Virtual Adapter流量
Add-Content $LogFile ""
Add-Content $LogFile "[Hyper-V Virtual Network Adapter(*)\Bytes/sec]"
Get-Counter '\Hyper-V Virtual Network Adapter(*)\Bytes/sec' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
# CPU
Add-Content $LogFile ""
Add-Content $LogFile "[CPU]"
Get-Counter '\Processor(*)\% Processor Time' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
# 网络队列长度
Add-Content $LogFile ""
Add-Content $LogFile "[Network Output Queue Length]"
Get-Counter '\Network Interface(*)\Output Queue Length' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
# Hyper-V VM Switch丢包
Add-Content $LogFile ""
Add-Content $LogFile "[Hyper-V Switch Packets/sec]"
try {
Get-Counter '\Hyper-V Virtual Switch(*)\Packets/sec' |
Select-Object -ExpandProperty CounterSamples |
Format-Table InstanceName,CookedValue -AutoSize |
Out-String | Add-Content $LogFile
}
catch {
Add-Content $LogFile "Counter not available."
}
Add-Content $LogFile ""
Add-Content $LogFile "END"
这两个对于判断 Hyper-V 在 70 万 NAT 连接场景下是否存在 RSS/VMQ 瓶颈非常有帮助。
No Comments