SharePoint 2013 Warm-Up Script

A Warm-Up Script is essential for seldom used pre-production environments to avoid waiting for what seems like an eternity for the web applications to warm up.  For the past couple of years I have been using a warm-up script written by Ryan Dennis, who in turn based his script off one from Kirk Hofer.  The script worked great in my SharePoint 2010 farms, but had issues with our new 2013 farm.  The problem was the script could no longer return the default credentials (account the script is executed under), so I updated it to use the Invoke-WebRequest cmdlet after reading a blog post from Todd Klindt.

The script will iterate through all the Site Collections in each of the Web Applications as well as open any sites located in the warmup-extrasites.txt file.  I use this file to open Office Web App documents, access my ADFS server, as well as keeping SSRS servers active.

In this post I’ll first provide a copy of the script and will then illustrate how to execute it:

############################################################################
 #WarmUpScriptV2.ps1 - Enumerates all site collections in each web applications
 # for a 2013 SharePoint farm using the Invoke-WebRequest cmdlet using the default credentials.
 #
 # Script will also cycle through any sites in the $extrasitelistfile. For example I use it to
 # warm up the OWA 2013 servers by passing it the WopiFrame.aspx page with parameters to open a
 # PowerPoint, Word, and Excel document out of a SharePoint document library.
 #
 #Notes:
 # Script was developed using a combination of information from Ryan Dennis, Kirk Hofer's and Todd Klindt's Blogs:
 # http://kirkhofer.wordpress.com/2008/10/18/sharepoint-warm-up-script/
 # http://sharepointryan.com/2011/03/29/warming-up-sharepoint-2010-using-powershell/
 # http://www.toddklindt.com/blog/Lists/Posts/Post.aspx?ID=404
 #
 #Assumptions:
 #
 #Running on machine with SharePoint 2013 installed
 ############################################################################

cls

 Add-PsSnapin Microsoft.SharePoint.PowerShell
 $extrasitelistfile = 'D:\Scripts\SharePoint\WarmUp\warmup-extrasites.txt'

Start-SPAssignment -Global

# Iterates through the sites in each web application

 $apps = get-spwebapplication -includecentraladministration
 foreach ($app in $apps) {
 $sites = get-spsite -webapplication $app.url -Limit ALL
 foreach ($site in $sites) {
 write-host $site.Url;
 $r = Invoke-WebRequest -URI $site.Url -UseDefaultCredentials
 $r.StatusCode
 }
 }

# 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=Invoke-WebRequest -Uri $site -UseDefaultCredentials
 }
 }

Stop-SPAssignment -Global

In my example I have placed a copy of the Script and the warmup-extrasites.txt file in the D:\Scripts\SharePoint\WarmUp directory.  This is just a text file with a URL on each line and will look something like this:

extrasitestxt

The script requires no parameters and can be executed directly from a PowerShell cmd or ISE window:
warmUpScript1

Now that we’ve successfully tested the script the next step is to create a scheduled task and will run after start up and execute on a regular interval throughout the day.

The first step is to create a task in the Task Scheduler:

warmUpScript2

Fill out each of the tabs as follows:

warmUpScript3

warmUpScript4

In the Actions tab we’ll point to the PowerShell executable and add the path of the script as the argument:

  • Script path: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • Arguments: D:\Scripts\SharePoint\WarmUp\WarmUpScript.ps1

warmUpScript5

You can click “OK” leaving the other settings at the default to complete creating the task and provide the credentials for the task to run under.  Keep in mind the account needs access to all the site collections as well as permissions to execute a scheduled task on the server.

Now that the task has been created manually execute it and verify a successful completion.

warmUpScript6

Advertisements
Posted in PowerShell, SharePoint 2013
13 comments on “SharePoint 2013 Warm-Up Script
  1. How will this work if I have multiple WFE servers? Do I need to run it on each one? Or do I use the “-proxy” parameter so it only runs on the localhost. My WFE’s are load balanced.

    • jasonth says:

      Hi Jeremy,

      In my environment the hosts file for each of the wfe servers points to itself and then I schedule the script to execute on each load balanced WFE.

  2. Sharon Guo says:

    Thanks for the script!

    I have one question:

    When this script is scheduled as a task to run in Windows Task Scheduler, what kind of permission the user account running this task has to have? Does it need to be a SP farm admin? Or just rights to log on as a batch job and permission to use Powershell on SharePoint farm? Does it need to have any rights in SharePoint SQL databases? or other permission it needs? Is it best practice just to run it with a high privileged account rather than to figure out the minimal permissions for this user to run the task?

    Thanks in advance for your time!

    Sharon

    • jasonth says:

      I run it with the admin/install account, but it will need rights to logon as a batch job and read permissions for each of the site collections as well as the extra sites listed in the text file.

  3. Chris says:

    Hi,
    I get the following error when running this.

    Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.

  4. Dean says:

    The script is great.

    But I found that even after running the warm up script. It still takes around 10 seconds to load the home page of a site(This page is already loaded in the warm up script). If I load the page in IE for the first time after the application pool is recycled, it takes quite long time. But the second time is almost instant. So It looks to me that the warm up script only loads the partial page. There are something else which can be done in IE but not in the script. Any suggestion on this.

    • jasonth says:

      Hi Dean,

      It may be that you have multiple web front ends behind a load balancer. We run the warmup script from each server and that server has an entry for each web application that points to itself. This forces the script to execute against the local host and since we run it on each server the application pools/sites are always warm.

      Do you have multiple wfe’s behind a load balancer and if so where are you executing the script from?

  5. Fabio says:

    Can I ask you why would you run it every 15 minutes? I mean isn’t it enough to run it once after the IIS application pool recycle?

    Thanks.

    • whorn76 says:

      After periods of inactivity an IIS site will go to ‘sleep’. When users try to access this site then they will be greeted with a several second wait for the site to spin back up. It may not be an issue with a highly used SharePoint site, but running the wake up script every 15 minutes is just a small thing to ensure optimal responsiveness at all times.

  6. Keth says:

    How do I save the Output of this script to a file? Because of the WriteHost cmd, the output file always only has the status and not the Url that’s been warmed up.

Leave a Reply

Fill in your details below or click an icon to log in:

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

%d bloggers like this: