随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

性能计数器取网卡流量

1.通过性能计数器取网卡流量

复制代码
$public_interface = "Broadcom BCM5709C NetXtreme II GigE [NDIS VBD 客户端]"

$counter = New-Object Diagnostics.PerformanceCounter
$counter.CategoryName = "Network Interface"
$counter.InstanceName = $public_interface
#$counter.CategoryName = "Processor Information(_Total)"

#添加计数器属性值,此处监视可用内存
$counter.CounterName = "Bytes Sent/sec"
#$counter.CounterName = "Bytes Received/sec"

#$counter.nextsample()  #取样本(RawValue),需要间隔1秒,取两个sample做减法,才能得出每秒流量
#获取当前计数器的值
$value = $counter.NextValue()
Start-Sleep -Seconds 1
$counter.NextValue()
复制代码

 

2.通过get-counter取网卡流量(PS2.0)

 #$networkcounter = get-counter -counter "\Network Interface(Broadcom BCM5709C NetXtreme II GigE [NDIS VBD 客户端])\Bytes Sent/sec" #-continuous
 $networkcounter = get-counter -counter "\Network Interface($public_interface)\Bytes Sent/sec" #-continuous
 ($networkcounter.CounterSamples|Select CookedValue).CookedValue

 

同时取网卡发送/接收流量

复制代码
#检查机器上配置IP的网卡数量,配置了gateway的网卡为Public,否则为Private
[array]$netifs = gwmi Win32_NetworkAdapterConfiguration -Filter "IPEnabled='true'"
If ($netifs.count -eq 1)
    {$public_interface = $netifs[0].Description.replace("(","[").replace(")","]").replace("#","_")}
If ($netifs.count -eq 2)
    {
     $public_interface = ($netifs | ? {$_.DefaultIPGateway -ne $null}).Description.replace("(","[").replace(")","]").replace("#","_")
     $private_interface = ($netifs | ? {$_.DefaultIPGateway -eq $null}).Description.replace("(","[").replace(")","]").replace("#","_")
    }

#定义函数,通过性能计数器取网卡发送/接收流量
Function Netif_Sent_Received($interfacename,$send_member,$receiev_member)
{
$counter_Sent = New-Object Diagnostics.PerformanceCounter
$counter_Receive = New-Object Diagnostics.PerformanceCounter
$counter_Sent.CategoryName = "Network Interface"
$counter_Receive.CategoryName = "Network Interface"
$counter_Sent.InstanceName = $interfacename
$counter_Receive.InstanceName = $interfacename
$counter_Sent.CounterName = "Bytes Sent/sec"
$counter_Receive.CounterName = "Bytes Received/sec"
$value_Sent = $counter_Sent.NextValue()
$value_Receive = $counter_Receive.NextValue()
Start-Sleep -Seconds 1
$Sent = $counter_Sent.NextValue()
$Receive = $counter_Receive.NextValue()
$i_object | Add-Member -MemberType NoteProperty -name $send_member -value $sent
$i_object | Add-Member -MemberType NoteProperty -name $receiev_member -value $receive
}


#将所有结果存放到$i_object对象中
$i_object = New-Object system.object
If ($private_interface -ne $null)
    {Netif_Sent_Received $private_interface Private_Send Private_Receive}
If ($public_interface -ne $null)    
    {Netif_Sent_Received $public_interface Public_Send Public_Receive}
复制代码

 

posted on   momingliu11  阅读(1558)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2014-08-07 在SCVMM2012R2中删除失去联系的VM GateWay
2014-08-07 get-random生成电话号码
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示