Solved

PowerShell Script Resetting IIS

Posted on 2015-01-28
13
178 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
[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
  • 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
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…

726 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