?
Solved

PowerShell Script # of Samples Help

Posted on 2015-02-12
9
Medium Priority
?
128 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 82

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 70

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 70

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 70

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
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…
Suggested Courses

765 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