Solved

PowerShell Script Resetting IIS

Posted on 2015-01-28
13
167 Views
Last Modified: 2015-01-29
I'm very new to PowerShell. I want to create a script that will reset IIS when CPU gets above 75% I found this script below and saved it in a .ps1 file.

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

My question is, how can I test to see if this works and where would I place this script so it will run when CPU hits 75%?

I was reading up on -WhatIf to test but when I add -WhatIf at the end of my script, powershell does not like that.

My server is Windows 2008 R2.

Thank you
0
Comment
Question by:BuRinger7a
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 40575668
To test - you could reduce the value to a lower %.
To execute it, you'd need to launch it frequently throughout the day - a scheduled task should suffice.  Just create it to run every 5 - 15 minutes (or whatever duration you're most comfortable with)
0
 
LVL 53

Expert Comment

by:Will Szymkowski
ID: 40575674
What I would recommend is using this in a scheduled task. The only problem is that you need to have this continually running so that it can monitor the CPU usage.

- Open Task Scheduler
- Create a New Task (give it a name)
- Triggers Should be Daily, Repeat task every 5 minutes, for Duration Select Indefinitely
- Under Actions Start a Program> Program/Script = powershell.exe > Add arguments =  -file "c:\scripts\scriptname.ps1"

Add any other conditions you want.

I would also make sure that you test this as a scheduled task first then set the Duration Indefinitely for repeating.

Will.
0
 
LVL 12

Expert Comment

by:David Paris Vicente
ID: 40575723
You can create a task scheduler to call your script based in your criteria.

To make a test with the CPU with 75% or more you can use this powershell code:
 ForEach ($Number in 1..4){
     start-job -ScriptBlock{ $result = 1; foreach ($number in 1..2147483647) {$result = $result * $number }}}

Open in new window


Don't forget to stop the job after all your tests with the Stop-job cmdlet or with task manager.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:BuRinger7a
ID: 40575771
I'm performing a full virus scan to get the CPU level up for testing. In Task Scheduler it says it ran and that the operation completed successfully.

When I go into event viewer, I do not see any logs on restarting the IIS services.
0
 

Author Comment

by:BuRinger7a
ID: 40576084
The script works when I run it in PowerShell. I get the event logs that the services stopped and then started. But when it runs in Task Scheduler, it stays in Running mode until the 5 minutes are up and it then says it finished successfully but no logs were generated.
0
 
LVL 53

Expert Comment

by:Will Szymkowski
ID: 40577170
Are you running it from the server itself? Or are you running the script remotely with the task scheduler?

Will.
0
 

Author Comment

by:BuRinger7a
ID: 40577317
I'm running it from the server itself.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40577325
How did you set it up?
The command should be powershell.  Set the arguments as -file <path to script>
You may also want to set the execution policy as an argument, unless you're certain it's correct.
0
 

Author Comment

by:BuRinger7a
ID: 40577333
Program/Script
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments
"d:\scripts\test.ps1"
0
 
LVL 53

Expert Comment

by:Will Szymkowski
ID: 40577354
Added Arguments need to be -file "d:\scripts\test.ps1".

I had stated this in my first post.

Will.
0
 

Author Comment

by:BuRinger7a
ID: 40577360
I should of mentioned that I also tried -file "d:\scripts\test.ps1"

Everything runs successfully in Task Scheduler but the services do not reboot.
0
 
LVL 53

Accepted Solution

by:
Will Szymkowski earned 500 total points
ID: 40577399
What you could do instead is call the powershell from a batch file. This is away around launching powershell directly from the task scheduler.

Launch Powershell from Batch file

Will.
0
 

Author Comment

by:BuRinger7a
ID: 40578196
Will,

Creating the batch file worked! Here is how I created the batch.
PowerShell.exe -executionpolicy unrestricted -file D:\scripts\test.ps1

In Task Scheduler
Programs/Script:
D:\Scripts\test.bat

Thank you!
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

A safe way to clean winsxs folder from your windows server 2008 R2 editions
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

770 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