Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

PowerShell Script Resetting IIS

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
BuRinger7a
Asked:
BuRinger7a
  • 6
  • 4
  • 2
  • +1
1 Solution
 
sirbountyCommented:
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
 
Will SzymkowskiSenior Solution ArchitectCommented:
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
 
David Paris VicenteSystems and Comunications Administrator Commented:
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
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
BuRinger7aAuthor Commented:
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
 
BuRinger7aAuthor Commented:
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
 
Will SzymkowskiSenior Solution ArchitectCommented:
Are you running it from the server itself? Or are you running the script remotely with the task scheduler?

Will.
0
 
BuRinger7aAuthor Commented:
I'm running it from the server itself.
0
 
sirbountyCommented:
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
 
BuRinger7aAuthor Commented:
Program/Script
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments
"d:\scripts\test.ps1"
0
 
Will SzymkowskiSenior Solution ArchitectCommented:
Added Arguments need to be -file "d:\scripts\test.ps1".

I had stated this in my first post.

Will.
0
 
BuRinger7aAuthor Commented:
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
 
Will SzymkowskiSenior Solution ArchitectCommented:
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
 
BuRinger7aAuthor Commented:
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

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now