Thursday, 24 October 2013

Keep IIS App Pools warm with PowerShell script

Happy Friday!

Due to the fact that our CI deploys the site every single check-in, very often nobody actually bothers loading up the pages afterwards to make sure those are operational. We work on applications that take sometimes even 5-10 min to come back up after deployment (eg. Sharepoint).

Anyway, I decided it would be a good idea to have a script running every 5 min on one of the servers that will actually try loading pages for us. This in theory means that pools will be warm and ready to go and you will not be stuck with forever loading browser window.

Below is my PowerShell script that will do the job. You will also need XML file that will hold all URLs.

Your XML needs to follow structure below. It can contains as many URLs as you require.
<Urls>
<Url>http://test.com</Url>
<Url>http://test1.com</Url>
</Urls>


param 
param 
(
    [string]$ConfigFile = "config.xml",
    [string]$TransactionFile = "lastRun.log"
)


# Get Config file
$0 = $myInvocation.MyCommand.Definition
$env:currentDir = [System.IO.Path]::GetDirectoryName($0)
$env:parentFolder = Get-Item $env:currentDir | Split-Path -Parent

[xml]$cfg = Get-Content (Join-Path $env:currentDir $ConfigFile)

if($cfg -ne $null){
    $wc = new-object net.webclient;  
    $wc.credentials = [System.Net.CredentialCache]::DefaultCredentials;  

    Start-Transcript (Join-Path $env:currentDir $TransactionFile)

    Foreach($url in $cfg.Urls.Url){
        if($url -ne $null){
            try{
                Write-Host "Warming up $url"
                $pageContents = $wc.DownloadString($url);
            }catch{
                Write-Host "Warming up of $url has timed out!"
                #throw $_
            }
        }
    }

    Stop-Transcript 
    $wc.Dispose();
}

Configuration steps

Fig 1
Once you have both XML config file and ps1 script file in one folder we can setup Task Scheduler.


  1. Open Task Scheduler
  2. Right click to "Create New Task"
    1. Press "Change User or Group" button 
    2. Press "Advanced..." button
    3. Press "Find" and Select "NETWORK SERVICE"
    4. Fig 2
      1. We are doing this so that this script runs under a different account (system account) otherwise every time you change your password you have to update this job.
    5. Tick "Run with highest privileges"
  3. Change Tab to Triggers
    1. Press "New..." - here you setup your trigger. (ie. what causes this to run)
    2. In my case I wanted this to run every 5 min. So I have ticked "Repeat task every" and selected "5 min" then select for duration of "Indefinitely"
    3. I set it to run "One time", however due to the "Repeat task every 5 min" it actually runs once and then repeats indefinitely.
  4. Change Tab to Actions
    1. This is where we set ps1 to run by pressing on "New..."
    2. Fig 3
      1. Select action to be "Start a program"
      2. Program script to be "PowerShell.exe"
      3. Under Add arguments (optional) enter "-ExecutionPolicy Bypass -File D:\KeepWarm\keepwarm.ps1 -Path D:\KeepWarm". Change your paths accordingly.
  5. Press OK to close
  6. Press OK to finish setting up this task.
  7. Right click task you just created and click "Run"
  8. Open Explorer and browse to a folder where you keep your keepWarm.ps1 and see if there is a new file there called "lastRun.log". 
  9. You can open this file to see last run results.
  10. You are done!

No comments :

Post a comment