Warming up SharePoint 2010 using PowerShell


If you’ve been around SharePoint 2007, SharePoint 2010 or just .NET for any amount of time, you know that pages are not compiled until the first time a server request is made to view them. This causes the first load to be the slowest.

There have been numerous solutions for this, most of them are some form of a warm-up script.

In SharePoint 2007, there were a few versions; all of which used the command-line STSADM tool to basically enumerate all sites in a web application and then submit an HTTP request to cache the pages on the server side.

In SharePoint 2010, the STSADM based scripts still work; but why use them? We have PowerShell!

I found some really good examples of PowerShell based Warm-up scripts, but none of them served my purposes or my clients. Since I’m really starting to dig PowerShell, I decided to do my part in the SharePoint community by developing my own version of the SPWarmup script.

#################################################################################
## SPWarmUp.ps1 - Enumerates all web sites in web applications in a SharePoint  #
## 2010 farm and opens each in a browser.  This version does not use STSADM	#
## and instead uses PowerShell.							#
## 										#
## Notes:									#
## -"get-webpage" function borrowed from:					#
## http://kirkhofer.wordpress.com/2008/10/18/sharepoint-warm-up-script/		#
##										#
## Assumptions:									#
## -Running on machine with SharePoint 2010 installed				#
## Although bits and pieces were borrowed from Kirk Hofer and Martin Laukkanen,	#
## The final working version with nice enhancements is courtesy of Ryan Dennis.	#
## www.iccblogs.com/blogs/rdennis - Twitter: @SharePointRyan			#
#################################################################################
  
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

# By uncommenting the following lines as well as lines 54-60 you can warmup other sites such as SSRS #
#$extrasitelistfile = 'c:\warmup-extrasites.txt'

Start-SPAssignment -Global  
function get-webpage([string]$url,[System.Net.NetworkCredential]$cred=$null)
{
   $wc = new-object net.webclient
   if($cred -eq $null)
   {
     $cred = [System.Net.CredentialCache]::DefaultCredentials;
   }
   $wc.credentials = $cred;
   return $wc.DownloadString($url);
}
  
#This passes in the default credentials needed. If you need specific
#credentials you can pass them in to elevate the permissions.
#Or run this task as a user that has a Policy above all the Web
#Applications with the correct permissions
  
$cred = [System.Net.CredentialCache]::DefaultCredentials;
#$cred = new-object System.Net.NetworkCredential("username","password","machinename")
  
$apps = Get-SPWebApplication -IncludeCentralAdministration
foreach ($app in $apps) {
$sites = Get-SPSite -WebApplication $app.url -Limit ALL -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
foreach ($site in $sites) {
Write-Host $site.Url;
$html=get-webpage -url $site.Url -cred $cred;
}
}

## Warm up other sites specified in warmup-extrasites.txt file (such as SSRS)
#if (test-path $extrasitelistfile) {
#   $extrasites = get-content $extrasitelistfile
#   foreach ($site in $extrasites) {
#     write-host $site;
#     $html=get-webpage -url $site -cred $cred;
#   }
#}

Stop-SPAssignment -Global
Advertisements

7 thoughts on “Warming up SharePoint 2010 using PowerShell

  1. nlvraghavendra

    When I ran the script I got the error – “Exception calling “DownloadString” with “1” argument(s): “The remote server returned an error: (503) Server Unavailable”.

    Any idea?

      • Ryan Dennis

        This could be for a number of reasons:
        1. Are you running it on one of the SP Servers?
        2. Does the account you’re running as have full read to all web apps?
        3. Are the sites up and running?
        4. Is it SP2010?

        I would test browsing to the sites from the server, and if that works the script should work – but the account will need permissions.

        Ryan

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s