Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PowerShell Script Resetting IIS

Posted on 2015-01-28
13
Medium Priority
?
250 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

610 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