Measure-Object -sum all counterSamples from contin

2019-06-07 19:25发布

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条回答
可以哭但决不认输i
2楼-- · 2019-06-07 19:58

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
查看更多
登录 后发表回答