Solved

PowerShell Script Resetting IIS

Posted on 2015-01-28
13
161 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:BuRinger7a
Comment Utility
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
Comment Utility
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
Comment Utility
Are you running it from the server itself? Or are you running the script remotely with the task scheduler?

Will.
0
The curse of the end user strikes again      

You’ve updated all your end user’s email signatures. Hooray! But guess what? They’re playing around with the HTML, adding stupid taglines and ruining the imagery. Find out how you can save your signatures from end users today.

 

Author Comment

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

Expert Comment

by:sirbounty
Comment Utility
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
Comment Utility
Program/Script
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

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

Expert Comment

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

I had stated this in my first post.

Will.
0
 

Author Comment

by:BuRinger7a
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now