Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PowerShell Script # of Samples Help

Posted on 2015-02-12
9
Medium Priority
?
131 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 83

Expert Comment

by:David Johnson, CD, MVP
ID: 40606146
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
ID: 40606269
David,

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

Expert Comment

by:Qlemo
ID: 40607592
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:BuRinger7a
ID: 40614939
Thank you Olemo. I will finally able to test this tomorrow and let you know how it worked.
0
 

Author Comment

by:BuRinger7a
ID: 40614947
Olemo,

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

Expert Comment

by:Qlemo
ID: 40615017
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
ID: 40625769
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 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40626349
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
ID: 40628722
Thanks Olemo. I tested your script through different scenarios and it works great!
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

604 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