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"
Michael McGovernVP of Information TechnologyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroEE Rookie/Solution Guide/Topic Advisor and CEO Faru Bonon ITCommented:
Hello Michael, I think you have the idea :)


Import-Module WebAdministration

$AllSites= Get-ChildItem -Path IIS:\Sites 

$AllSites| foreach { 
    write-host -ForegroundColor Cyan "Stopping all sites"
    Stop-WebSite $_.Name 
} 

#debug text
<#
for($i=0;$i -le ($AllSites.count)/50; $i++){
    write-host -ForegroundColor Cyan "Starting sites ($i*50) -> $( (($i+1)*50)-1  ) opping all sites"
}

$AllSites=@()
for($i=0;$i-lt600;$i++){
    $AllSites += new-object -TypeName psobject -Property @{"name"=$i;"a"=$i*50;}
}
#>

#how many sites you want to enable at the time.
$div=50

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"
    #debug 
    <#
    $a.count
    $a | select -First 1
    $a | select -last 1
    #>

}
 

Open in new window


I've left all the debug code I used to simulate the 600 sites.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jose Gabriel Ortega CastroEE Rookie/Solution Guide/Topic Advisor and CEO Faru Bonon ITCommented:
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

Charlie ArehartTroubleshooting ConsultantCommented:
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.
Michael McGovernVP of Information TechnologyAuthor Commented:
Thank you Jose for the script. It works like a charm.
Jose Gabriel Ortega CastroEE Rookie/Solution Guide/Topic Advisor and CEO Faru Bonon ITCommented:
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.