Monday, May 6, 2013

Keep a website alive – PowerShell style

 

Recently, We had a website that didn’t have frequent visitors, but when the visitors did come, the website would take a long time to load up the first time. This is expected behavior because IIS shuts down its worker threads.

1 approach would be to set the IdleTimeout to 0 which means the threads are never aborted (details here: http://technet.microsoft.com/en-us/library/cc771956(v=ws.10).aspx). Instead of that though I decided to try my hand at PowerShell and came up with the following script:

   1:  # List of URLS to Ping
   2:  $urls = @("Http://URL1.com", "https://URL2.com")
   3:   
   4:  #Ping all URLs in the list
   5:  foreach ($objItem in $urls) {
   6:      $req=[system.Net.HttpWebRequest]::Create($objItem);
   7:      $res = $req.getresponse();
   8:      $stat = $res.statuscode;
   9:      $res.Close();
  10:      
  11:      #Pump it out to a text file
  12:      $res | Out-File pingresults.txt -append 
  13:  }



After that I set up a simple scheduled task to execute this at 15 minute intervals during business hours and walk away. Since I can schedule this when users are likely to hit the site, I have a customized “keep awake” script that provides more flexibility.


Enjoy!

No comments:

Post a Comment