当前位置: 动力学知识库 > 问答 > 编程问答 >

powershell - Parsing Get-Counter data to just get the values

问题描述:

I would like to get the total number of bytes that my computer has sent/received over some interval.

Powershell has a handy cmdlet that gets me access to that information (Qualcomm Atheros AR9285 Wireless Network Adapter is the name of my interface):

Get-Counter -Counter "\Network Interface(Qualcomm Atheros AR9285 Wireless Network Adapter)\Bytes received/sec" -Continuous

It gets the data just fine, but it comes out like this:

The closes I could get to having it come out the way I wanted was using Get-Counter -Counter "\Network Interface(Qualcomm Atheros AR9285 Wireless Network Adapter)\Bytes received/sec" -Continuous | Format-Table -Property Readings but that still had a long path name in front of the value I want.

Additionally, if I try setting the output to be some variable, no assignment ever gets done (variable stays null).

How can I get this data in a decent format?

Note: The goal is to keep a running tally of this information, and then do something when it reaches a threshold. I can do that if I can get the above to work, but if there is a better way to do it, I am more than happy to use that instead.

网友答案:

Pipe to Foreach-Object and get the value from the CounterSamples property. CounterSamples is an array and the value is in the first item:

Get-Counter -Counter "\Network Interface(Qualcomm Atheros AR9285 Wireless Network Adapter)\Bytes received/sec" -Continuous |
Foreach-Object {$_.CounterSamples[0].CookedValue}
分享给朋友:
您可能感兴趣的文章:
随机阅读: