Measure-Object -sum all counterSamples from continuous get-counter

。_饼干妹妹 提交于 2020-01-23 18:54:20

问题


I am trying to write a PowerShell script that will continuously poll a performance counter every N seconds then sum the values returned by the counter. My end goal is to have results for a dozen or so counters get rolled up and shipped off to a Graphite server for monitoring and reporting.

so far this is what I have cobbled together for a particular counter, i'm just not sure how to get a couple of things in the land of PowerShell magic voodoo.

  1. I can't figure out how to get the Job ID as an integer so I can automate the while loop.
  2. Return only CounterSample data from Receive-Job that can be piped to Measure-Object to get a sum.

    Start-Job {Get-Counter -Counter "\Network Interface(MyNic)\Bytes Received/sec" -Continuous -SampleRate 1}
    
    while ($true) { 
            start-sleep -s 10
            Receive-Job -id N
    }
    

I would also love to know a simple or effective way to dynamically determine the active NIC on a windows box in PowerShell v1.0 or v2.0. "\Network Interface(*)\" works but gives me everything.


回答1:


Regarding #1, grab the InstanceId returned from Start-Job. You can use that later to refer to the job e.g.:

$job = Start-Job ...

And for #2, add an extra foreach at the end e.g.:

$job = start-job {Get-Counter -Counter "\Network Interface(Realtek PCIe GBE Family Controller)\Bytes Total/sec" -Continuous -SampleInterval 1 | Foreach {$_.CounterSamples}}

Then sum the data like so:

Receive-Job $job | Measure CookedValue -Sum


来源:https://stackoverflow.com/questions/11910996/measure-object-sum-all-countersamples-from-continuous-get-counter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!