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.
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