问题
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.
- I can't figure out how to get the Job ID as an integer so I can automate the while loop.
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