Solved

PowerShell Script # of Samples Help

Posted on 2015-02-12
9
124 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 81

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your 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 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 500 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

724 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