Yet Another Powershell Warmup Script for Sharepoint

The title says it all ! I am publishing in this article a Powershell script I created that I use in my Sharepoint Farm to warmup my sites after there has been a recycle of the application pools that host my Sharepoint Web Applications. It is a good practice to schedule a recycle of the web sites on a daily basis, ideally this should happen during non working hours. The issue then is that when first users access the portal they experience slow load times because it's the first time the pages are accessed.


What this script does to mitigate this issue is to create a request to the home page of every site in a given Web Application. The idea is to copy the script on every Web Front End server in our Sharepoint Farm and configure a Windows Scheduled Task to run after the application pools have been recycled (recycle schedule settings can be accessed in the Application Pool properties of Internet Information Services).


Prerequisites

There are however two configuration settings required on the server before the following script can be run.

  • We need to make sure that when the Web Front End server creates a request for a particular Web Application this is being served by the same server. While this will always be true if there is only one WFE in our Farm, it won't if we have multiple load balanced servers. In that scenario we need to update the HOSTS file (that can be found in the c:\Windows\System32\Drivers\etc folder) and add a new entry that maps every web application DNS name to the current server
  • With .Net Framework 3.5 SP1 Microsoft included an additional security feature to prevent reflection attacks to web servers. This feature returns a 401.1 Unauthorized messages when a FQDN or a custom host header is used to browse a site hosted in the server. This will effectively prevent the warmup script from creating requests to the web applications of our Farm. There are two different options to overcome this problem, the correct solution would be to add all our web applications to the exclusion list of the security check, however this can get difficult if we have many web applications and many WFE servers in our farm (a script might help there!). Another solution is to disable the check, this can be done by creating a new DisableLoopbackCheck registry entry in the server. Click here for a detailed description of both solutions.


Add-PSSnapin Microsoft.Sharepoint.PowerShell
function ProcessWebApplication([string]$url)
{
 Write-Host "Processing Web Application....." $url
 Write-Host
 $wc = new-object net.webclient
 $wc.credentials = [System.Net.CredentialCache]::DefaultCredentials;
 $wa = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url);
 Foreach($site in $wa.Sites)
 {
  Write-Host "Processing Site Collection..... " $site.Url
  
  ProcessWeb($site.OpenWeb())
  Write-Host
 }
}

function ProcessWeb([Microsoft.SharePoint.SPWeb]$web)
{
   $s = $wc.DownloadString($web.Url)
   Write-Host $web.Url ".... Ok!"
   Foreach($child in $web.GetSubWebsForCurrentUser())
   {
 ProcessWeb($child)
   }
}
# Replace with your web applications
ProcessWebApplication("http://application1")
ProcessWebApplication("http://application2")

To finish with the article, I will publish the code I use to schedule the execution of the script. To do so, I create a .BAT file I place in the same folder as the Powershell script file (warmup.ps1) and I create a Windows Scheduled task that executed this process. It is important to make sure the Scheduled Task runs under an account that has permissions on all the Web Applications our script will access.


powershell -command "& 'c:\warmup\warmup.ps1'"


Created on 25/05/2011



Get Microsoft Silverlight

Get Microsoft Silverlight