Solved

PowerShell Script # of Samples Help

Posted on 2015-02-12
9
105 Views
Last Modified: 2015-02-24
I have a PowerShell script that will reset IIS if CPU hits 90%. In Task Scheduler, I created this task to run every 10 minutes.
I would like the script to also include 3 samples of 5 minute intervals. If CPU is still above 90%, I want IIS to reset.
Here is what I have now and it works. I need help adding the samples and minutes.
I'm running Windows 2008 R2.

if ((Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples[0].CookedValue -gt 90) { &iisreset }

Thank you
0
Comment
Question by:BuRinger7a
  • 5
  • 3
9 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
you will have to set your runtime to > 10 minutes or only allow it to  run 1 copy of the script

function test-cpu 
    {
    Get-Counter ('\Processor(_Total)\% Processor Time').CounterSamples[0].CookedValue
    }

$count = 0
while (test-cpu -ge 90){
    $count ++
    start-sleep -Seconds 600
    if ($count -ge 3)
        {
        &iisreset 
        }
}

Open in new window

0
 

Author Comment

by:BuRinger7a
Comment Utility
David,

Line 10, what does -ge 3 represent?
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
The above will wait until CPU has gone 3 times over 90%, but not in a row. It could be 1 hour ago and 15 minutes ago and now, and it would still hit. Worse, it is in an endless loop until the restart condition applies.
This will get 3 successive samples, and require all of them to be "over the top".
Get-Counter '\Processor(_Total)\% Processor Time' -MaxSamples 3 -SampleInterval 5*60 |
  select -Expand CounterSamples |
  ? { $_.Cookedvalue -gt 90 } |
  measure-object | ? { $_.Count -eq 3 } |
  % { &iisreset  }

Open in new window

BTW, another way to perform that task is to use a (Performance Counter) Data Collector Set triggering the IIS reset action ...
0
 

Author Comment

by:BuRinger7a
Comment Utility
Thank you Olemo. I will finally able to test this tomorrow and let you know how it worked.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:BuRinger7a
Comment Utility
Olemo,

Your script will give me 3 samples of 5 minutes each?
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
3 successive samples, each 5 minutes apart, so waiting 10 minutes overall (sample, wait, sample, wait, sample). All samples need to be over 90%.
0
 

Author Comment

by:BuRinger7a
Comment Utility
Olemo,

Here is what I have in the script you provided and attached is the error.

Get-Counter '\Processor(_Total)\% Processor Time' -MaxSamples 3 -SampleInterval 5*60 |
  select -Expand CounterSamples |
  ? { $_.Cookedvalue -gt 90 } |
  measure-object | ? { $_.Count -eq 3 } |
  % { &iisreset  }
powershell.gif
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
Grrrr, I hate PowerShell doing that (considering numerical expressions to be a string).
Get-Counter '\Processor(_Total)\% Processor Time' -MaxSamples 3 -SampleInterval 300 |
  select -Expand CounterSamples |
  ? { $_.Cookedvalue -gt 90 } |
  measure-object | ? { $_.Count -eq 3 } |
  % { &iisreset  }

Open in new window

0
 

Author Comment

by:BuRinger7a
Comment Utility
Thanks Olemo. I tested your script through different scenarios and it works great!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Join & Write a Comment

A procedure for exporting installed hotfix details of remote computers using powershell
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now