0

Hi I'm working on a script to take samples of the processor over a period of time right now I'm using just 5 seconds to run it quickly for testing... I'm not to familiar with powershell but found a few things between this site and others to come up with this, which works on my Windows 7 machine but not on windows 2008 R2, once I get the average I'll be placing it in an if statement to send an alert if the processor becomes to bogged down. I'd appreciate any help with this matter and thanks ahead of time....

$a=Get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5

($a.CounterSamples.CookedValue | Measure-Object -Average).average
3
  • 2
    What exactly is your question? As for why it might work on one machine but not another, see my answer to this question. Commented Feb 14, 2014 at 4:55
  • My understanding is that your code works on Seven, but not on W2K8R2. According to @mike z comment, what are the PowerShell version that you are using on each macine ? Commented Feb 14, 2014 at 5:05
  • yes totally forgot about the different versions I assumed they were both the same, the code from the answer below does work which is what I was after but good to know about the different versions between the two systems, thanks for your help.... Commented Feb 14, 2014 at 17:46

2 Answers 2

0
$b = Get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5|select -expandProperty countersamples|select -ExpandProperty cookedvalue|Out-String

$($b[0]+$b[1]+$b[2]+$b[3]+$b[4])/5

This should work on V2.

Sign up to request clarification or add additional context in comments.

7 Comments

I forgot about the different versions and yes this code does work thank you so much....
For anyone else taking this approach just wanted to add to this that the processor can return 0 values which get's some weird results when the division comes into play, for some reason I was receiving two values when dividing and on occasion would get an error regarding the 0 division so tally up the array and check for the value to be above 0 before dividing
Ok so it's not because of zero, I bumped up the samples but when I add them all up together instead of one number I'm getting an array of 3 numbers like this: $b = Get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 2 -MaxSamples 60 |select -expandProperty countersamples|select -ExpandProperty cookedvalue|Out-String $a=($b[0]+$b[1]+$b[2]+$b[3]+$b[4]+$b[5]+$b[6]+$b[7]+$b[8]+$b[9]+$b[10]+$b[11]+$b[12]+$b[13]+$b[14]+$b[15]+$b[16]+$b[17]+$b[18]+$b[19]+$b[20]+$b[21]+$b[22]+$b[23]+$b[24]+$b[25]+$b[26]+$b[27]+$b[28]+$b[29]) 3.125 0.8780475426722 1.7543 Any ideas?
The 3 numbers after the brackets are what's coming back from the variable I put it in the line breaks didn't show up....
Threw in the towel and upgraded powershell Problems fixed, should have done it right away instead of trying to fix the error lol
|
0

$a=Get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 2 -MaxSamples 15 if(($a.CounterSamples.CookedValue | Measure-Object -Average).average -gt 90)

{

execute some type of alert script

}

Then all you have to do is use task scheduler to execute on a certain interval....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.