Link to home
Start Free TrialLog in
Avatar of Michael McGovern
Michael McGovernFlag for United States of America

asked on

Start IIS sites in batches

Hello,

We host over 600 IIS sites and after a framework deployment by the Web Developers, the CPU goes through the roof after IIS is started. What we would like to do is start IIS, stop all the sites, then start them up in batches (for example, 50 at a time) to see if it helps with controlling the CPU usage. Could someone assist with a powershell script that will start up sites in batches?

Import-Module WebAdministration
Get-ChildItem -Path IIS:\Sites | foreach { Stop-WebSite $_.Name; } "Stops all sites"
ASKER CERTIFIED SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Final version would be like:

[cmdletbinding()]
param(
    [Parameter(Position=0,Mandatory=$false)][switch]$start=$false,
    #how many sites you want to enable at the time.
    [Parameter(Position=1,Mandatory=$false)][switch]$div=50
)
#If start is false you're gonna stop the sites,
#if start is true you're goona start the sites 50 at the time (you can change it on div var and give it over console as parameter

Import-Module WebAdministration

$AllSites= Get-ChildItem -Path IIS:\Sites 
if($start){
    $AllSites| foreach { 
        write-host -ForegroundColor Cyan "Stopping all sites"
        Stop-WebSite $_.Name 
    } 
}
else{
    for($i=0;$i-le ($AllSites.count/$div); $i++){
        $start=$i*$div
        $end = (($i+1)*$div)-1
        Write-Host -ForegroundColor Cyan "Starting sites $start -> $end"
        $a = $AllSites | select -Skip $start -First $div
        $a | %{
            Start-WebSite $_.Name 
        }
        $x =Read-Host "Press any key to continue"
    }
}

Open in new window


To run it
Default, Stop all sites

.\Thenameyougiveit.ps1

Open in new window


Previous is the same but filling all parameters, Stop all sites (-div is not in use)
.\Thenameyougiveit.ps1 -start $false -div 50

Open in new window


Final use (Starting 25 sites at the time)
.\Thenameyougiveit.ps1 -start $true -div 25

Open in new window

Avatar of Charlie Arehart
Charlie Arehart

If this batching of site starts doesn't help, the next step would seem to be to find WHERE the high cpu was happening on IIS start. By that I mean simply what processes show up as high cpu in Task Manager.

We might presume it is the w3wp.exe for each site's app pool, but it may be different or otherwise helpful to know.
Avatar of Michael McGovern

ASKER

Thank you Jose for the script. It works like a charm.
Ohhh Nice!
I'm glad it worked :) I don't have any server with more than 10 sites on it hahaha
So I'm really glad it worked because it was untested I just simulated the results and then tried to make it work for your end :)

I'm so happy it worked like a charm and to help you! yeeey!