Solved

PowerShell Script # of Samples Help

Posted on 2015-02-12
9
112 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 80

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 69

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 69

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 69

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sqlldr issue 1 15
send only last errors on log files 4 21
Robocopy parameters. 6 17
Run Windows Server from USB 2 18
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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