Using Network Using Powershell

I want to calculate the bytes sent and received by a particular process. I want to use powershell for this. Something I can do using Resource Monitor-> Network Activity. How can I do this with get-counter?

+6
source share
2 answers

There is a really good Scripting Guy article about using the Get-Counter cmdlet here:

Scripting Guy - Get-Counter

The trick will be to find the counter that you need, and I don't think you can get the granularity that you use, since these are the same counters that PerfMon uses. This focuses more on the entire network interface than on individual processes using the interface. With that said, if this is the only thing that uses this interface, it should do the trick beautifully.

See the network interface options available to run:

 (get-counter -list "Network Interface").paths 
0
source

You can't, it seems. I absolutely can not find the counters that the performance monitor works with, although other people can happen. There may be another way than get-counter, but this is what you specifically requested.

Looking through the counters, the closest you find are the IO Read Bytes / sec and IO Write Bytes / sec counters on the process object.

The problem is that they consider more than just network activity. The description in perfmon says:

"This counter counts all the I / O activity generated including files, networks, and I / O devices."

If you know that a process that you want to track only or basically write to a network connection may be better than not measuring anything.

You would do that (I will use Chrome as an example, since it works conveniently and uses data right now):

 get-counter "\Process(chrome*)\IO Read Bytes/sec" 

It just gives you a one-time read. If you want to continue reading, you can add a continuous switch.

The PerformanceCounterSampleSet returned object is not quite working, but you can find the actual read in $ obj.countersamples.cookedvalue.

The list will be quite long (if you are browsing like me). Chrome works in many separate processes, so we’ll do some math to compile them all and present them in KB.

Final result:

 get-counter "\Process(chrome*)\IO Read Bytes/sec" -Continuous | foreach { [math]::round((($_.countersamples.cookedvalue | measure -sum).sum / 1KB), 2) } 

Running this will simply continuously output the number of views that Chrome KB / s uses.

0
source

All Articles