Kirk Hofer’s Blog

October 18, 2008

SharePoint Warm Up Script

Filed under: SharePoint — Tags: , — kirkhofer @ 3:46 pm

I have had a lot of people use my other warm up script that I created almost a year ago but several people have asked for one that actually works…doh.  You see, my old one just used the WebRequest object to hit the site.  That does NOT warm up the site.  You actually need to get content from the page first.

This new script is a complete one that can be scheduled on your WFEs.  If you didn’t know already, all the Application Pools that get created are scheduled to reset themselves at a certain time.  Usually around 2 AM in the morning.  So if you want, take this script and put it in a CMD or BAT file and put an IISRESET right in front of it.  Then, add the code I have provided in a PS1 script and you are off and gunning.  You can remove those app pool recycles that are scheduled.

Save the following in a script named SPWarmUp.ps1

############################################################################
#Assumptions:
#-Running on machine with WSS/MOSS
#-C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN in path
############################################################################
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 stuff you can use something else to
#elevate basically 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")

[xml]$x=stsadm -o enumzoneurls
foreach ($zone in $x.ZoneUrls.Collection) {
    [xml]$sites=stsadm -o enumsites -url $zone.Default;
    foreach ($site in $sites.Sites.Site) {
        write-host $site.Url;
        $html=get-webpage -url $site.Url -cred $cred;
    }
}

When you schedule this to run through a scheduled task, make sure the user running this has the correct credentials on the sites being hit.  Obviously, make sure this is on ALL WFEs.

Can you test this without all the other stuff?  Sure, just copy and paste the code and put it in to PowerShell and give it a whirl.

Download the file here

Advertisement

44 Comments »

  1. Looks great, and thanks for the update, but I get the following errors when I try to run it.

    Cannot convert value “System.Object[]” to type “System.Xml.XmlDocument”. Error: “Data at the root level is invalid. Lin
    e 2, position 1.”
    At D:\admins\Scripts\WarmUpScripts\testwarmpup.ps1:27 char:8
    + [xml]$x= <<<< stsadm -o enumzoneurls
    Cannot convert value “System.Object[]” to type “System.Xml.XmlDocument”. Error: “Data at the root level is invalid. Lin
    e 2, position 1.”
    At D:\admins\Scripts\WarmUpScripts\testwarmpup.ps1:29 char:13
    + [xml]$sites= <<<< stsadm -o enumsites -url $zone.Default;

    Exception calling “DownloadString” with “1″ argument(s): “The path is not of a legal form.”
    At D:\admins\Scripts\WarmUpScripts\testwarmpup.ps1:16 char:27
    + return $wc.DownloadString( <<<< $url);

    I did a straight up copy and paste, I did not change anything. Just wanted you to know.

    Comment by rolandserman — October 20, 2008 @ 4:43 pm

  2. Actually, I just tried it on another of my WFE’s. Seems to work on Windows 2003, but not on my Windows 2008 WFE. The output above was from my w2k8 WFE, the other is still running.

    Any thoughts as to why it wouldn’t work on Windows 2008?

    One other question, we’re running host based site collections, so for example instead of a site collection being https://myurl.com/sites/sitecollection, they are https://sitecollection.myurl.com. The problem is, when you query SharePoint i.e. enumsites or whatever method you try to use, it lists them as http:// not https. Here in lies our problem, all of our content is over SSL, so any wake up script I’ve tried has failed on those host based site collections. Any thoughts on how to get around this?

    Thanks.

    Comment by rolandserman — October 20, 2008 @ 4:54 pm

  3. Try running the STSADM command separately on the w2k8 box. Looks like that might not be in the path.

    Kirk

    Comment by kirkhofer — October 20, 2008 @ 5:17 pm

  4. For the host-named site collections (stsadm -o createsite -hhurl …), you might have to add something to actually replace the http:// with https://.

    $url = $url.Replace(“http://”,”https://”);

    I actually have a PoSH script that will attempt to hit a site and see if it gets a valid response (Status=200). That in combination with something like this would probably work too. The key is, this could be the base for what you are trying to do so go ahead and make changes to it!

    Comment by kirkhofer — October 20, 2008 @ 5:46 pm

  5. I found the problem to my first post. Forgot to run powershell as administrator. Doh!

    Roland

    Comment by rolandserman — October 20, 2008 @ 5:47 pm

  6. Thanks, I’ll have to play with that when I get my test farm back up and running.

    Roland

    Comment by rolandserman — October 20, 2008 @ 6:22 pm

  7. [...] Powershell Warm Up Script I take no credit for this, I found it at Kirk Hofer’s blog, and thought I would share it as [...]

    Pingback by SharePoint Powershell Warm Up Script « Roland Serman’s Blog — October 30, 2008 @ 8:44 pm

  8. I have several sites on sharepoint using SSL and some not using SSL. How do I modify the script to work with both or do I need to have two scripts?

    Comment by Kat — March 24, 2009 @ 5:53 pm

    • You may have to use some more logic to call this with the URL you want. Like “warm-up-site -url https://siteurl“. That could be done easily

      Comment by kirkhofer — March 27, 2009 @ 9:59 pm

  9. Hey will this script warm up all the pages in all the sites in the site-collection for a publishing site? The reason for asking this question is generally the Publishing/Internet facing site are FBA based and dont use windows credentials, will the same script work in case of FBA auth?

    Comment by MB — March 25, 2009 @ 3:26 am

    • The main thing is loading the site collection in to memory in the w3wp process. Each page does not need to be ran through

      Comment by kirkhofer — March 27, 2009 @ 9:58 pm

  10. Great job. (Though your download link is broken.) This could also be extended by adding a “configuration” xml file that called specific pages to cache up a demo.

    I love the simplicity!

    Thanks!

    Comment by Matthew — September 29, 2009 @ 7:55 pm

  11. [...] PowerShell Warm-Up Script Found a really good one by Kirk Hofer here. If you do get an error when you run it (see [...]

    Pingback by SharePoint PowerShell Warm-Up Script « Harish Mathanan — October 22, 2009 @ 1:34 pm

  12. I get a ‘cannot resolve stsadm because it refers to term \ms\wse\12\bin\stsadm???

    How can I get round this error?

    Comment by AAAAAAArgh! — November 10, 2009 @ 8:54 am

  13. I can get the script to run of sorts.. I am in a 3 WFE farm (Win 2K8), but when I run on one of the WFEs I get errors stating:

    Exception calling “DownloadString” with “1″ argument(s): “The remote name could not be resolved: ‘##name of site##’ At E:\Scripts\SPWarmUp.ps1:14 char:28
    + return $wc.DownloadString( <<<< $url);

    This error appears for every site I have?

    Any ideas?

    Comment by Weird Errors — November 10, 2009 @ 9:36 am

  14. !!!…for all Admins with the German Version of MOSS 2007 !!!
    You must replace at line 25 the $zone.Default; with $zone.Standard;

    Comment by emdschie — January 25, 2010 @ 10:25 am

  15. Good brief and this mail helped me alot in my college assignement. Thank you for your information.

    Comment by WP Themes — February 15, 2010 @ 12:27 am

  16. [...] there are loads of examples out there to get you started as with most things PowerShell. See Kirk Hofer’s Blog for the one I started [...]

    Pingback by Project Server Blog » Warmup that farm — February 20, 2010 @ 5:32 am

  17. Hi,

    first of all thanks for this scripts, I tried different other scripts and they were not working in a scheduled task. Because of lack in Powershell knowledge it took me a while to get the script running smooth.

    The error mentioned from “Weired Errors”:

    Exception calling “DownloadString” with “1″ argument(s): “The remote name could not be resolved: ‘##name of site##’ At E:\Scripts\SPWarmUp.ps1:14 char:28
    + return $wc.DownloadString( <<<< $url);

    can be solved by ensuring that the Name of server (which is configured in "Alternate Access Mappings") matches the name in the certificate for SSL encryption.

    Another problem I have is that just the main pages are "warming up".
    I have plenty of document libraries, they are "warmed up", but if I stick into the content of a library, I still have to wait on first access.
    Example:

    main site (warmup ok)
    -sub site (warmup ok)
    –document libraries (warmup not ok)
    –document libraries (warmup not ok)
    –document libraries (warmup not ok)
    –document libraries (warmup not ok)
    –document libraries (warmup not ok)

    The user who is running the script has permissions to all pages and libraries.
    Any ideas how I can solve this?

    Regards,
    Markus

    Comment by Markus — March 17, 2010 @ 9:18 am

    • You would definitely need to get in to more logic to iterate through the “Lists” and then grab the SPList.RootFolder.WelcomePage and warm that up with similar calls.

      Comment by kirkhofer — March 18, 2010 @ 4:30 pm

    • Hi Markus
      Can you please explain a little bit how did you solve this?
      Exception calling “DownloadString” with “1″ argument(s): “The remote name could not be resolved: ‘##name of site##’ At E:\Scripts\SPWarmUp.ps1:14 char:28
      + return $wc.DownloadString( <<<< $url);

      can be solved by ensuring that the Name of server (which is configured in "Alternate Access Mappings") matches the name in the certificate for SSL encryption.

      Do you mean adding a server name (AAM) instead of $url?
      Thanks
      Waqar

      Comment by Waqar — October 26, 2010 @ 2:58 pm

  18. Have you created a sharepoint 2010 script similiar to this, or could I make some minor adjustments to this one?

    Comment by Chad — June 9, 2010 @ 12:37 pm

    • You can use the same one actually. Since STSADM might not be there in the future, I would look to use the Get-SPSite PowerShell CmdLet.

      Comment by kirkhofer — June 10, 2010 @ 5:22 pm

  19. We are using an app in the internet zone that authenticates against LDS. How can I modify this script to use a specific “username” & “password” as opposed to pulling in the ones from the shell ?

    Thanks

    John

    Comment by Johno — August 11, 2010 @ 4:40 am

    • You would probably have to create a function and call it. You could even do it within the PS1 file itself. Like Warm-up -site “http://blah” -username Kirk -password password

      Comment by kirkhofer — August 20, 2010 @ 2:02 pm

  20. Hi Kirk,
    i’m new to powershell,
    i just copied the code and pasted it into the powershell window,
    then got the following errors:

    Cannot convert value “System.Object[]” to type “System.Xml.XmlDocument”. Error: “Data at the root level is invalid. Lin
    e 2, position 1.”
    At D:\admins\Scripts\WarmUpScripts\testwarmpup.ps1:27 char:8
    + [xml]$x= <<<< stsadm -o enumzoneurls
    Cannot convert value “System.Object[]” to type “System.Xml.XmlDocument”. Error: “Data at the root level is invalid. Lin
    e 2, position 1.”
    At D:\admins\Scripts\WarmUpScripts\testwarmpup.ps1:29 char:13
    + [xml]$sites= <<<< stsadm -o enumsites -url $zone.Default;

    Exception calling “DownloadString” with “1″ argument(s): “The path is not of a legal form.”
    At D:\admins\Scripts\WarmUpScripts\testwarmpup.ps1:16 char:27
    + return $wc.DownloadString( <<<< $url);

    my moss site ultilizes form based authentication,and i use hostname to access it in the form (http://server1/)
    expect hearing from u.
    Thanks

    Andrew

    Comment by Andrew — October 28, 2010 @ 7:15 am

  21. [...] Ein gutes PowerShell-Skript für diese Zwecke kommt von Kirk Hofer. [...]

    Pingback by Warm Up Ergänzungen | SharePoint 2010 — November 16, 2010 @ 6:23 pm

  22. One of our site collections consists of a 160 GB content datatabase with multiple sites. This script works great for the smaller site collections, but times out on that larger site collection. Do you know if there is a way to either increase this timeout, or prevent the timeout from the SPWarmUp.ps1?

    Comment by Steve — January 14, 2011 @ 3:35 pm

    • First off, I would break that Site Collection up. Too big. I know, easier said than done. You would have to use the HttpWebRequest object to really do the timeout trick. Search the net for that one.

      Comment by kirkhofer — January 16, 2011 @ 3:11 pm

  23. Yes, I recommend changing the script above to use: [Kirk Hofer: I am adding this IF YOU ARE USING SHAREPOINT 2010. Plus, you need more like Get-SPSite -limit All]

    foreach ($site in Get-SPSite)
    {
    write-host $site.Url;
    $html=get-webpage -url $site.Url -cred $cred;
    #write-host $html.Length;
    }

    Comment by Peter — January 28, 2011 @ 5:54 am

  24. [...] Run a warm-up script daily after a recycle operation so that users don’t have to experience the website delay. SharePoint warm-up script  [...]

    Pingback by SharePoint Slow Load (SSL) issue « FYano's Tech Blog — April 13, 2011 @ 1:57 am

  25. [...] idea comes from Kirk Hofer’s blog, but modfied to use the SharePoint 2010 powershell commandlets and not care about specifying [...]

    Pingback by Easy SharePoint 2010 warmup Script using PowerShell | JonTheNerd — April 19, 2011 @ 7:17 pm

  26. Some additions: Filter for Get-SPSite and Dispose for the site collection object

    $sites = Get-SPSite https:///* -Limit ALL
    foreach($site in $sites)
    {
    #write-host $site.Url;
    $html=get-webpage -url $site.Url -cred $cred;
    #write-host $html;
    $site.Dispose();
    }

    Comment by Henrik Andersson — June 21, 2011 @ 9:19 am

  27. Hi Kirk,
    Thank you for sharing your warmup script. We have 3 WFEs servers and use NLB. All our site collections use Host headers, how can we be sure that the warmup script hit all WFEs? If I install the ps1 script in each WFE, and configure task scheduling to make it run on each WFE, it is sure that we hit all WFEs? (We are not allow to use HOST file on each WFE to map the URL with each local IP Address).
    Thank you in advance.
    Thanh-Nu

    Comment by Thanh-Nu Leroy — June 24, 2011 @ 1:25 pm

  28. It does not work in FBA environment, throws 403 forbidden error. Could you please provide script that would work in FBA environment.

    Comment by Anand Swami — July 5, 2011 @ 11:58 pm

    • Hey Anand,

      The 403 Forbidden error on FBA environments can be overcome by changing the WebClient call to a WebRequest call..

      like such..

      $request = [System.Net.WebRequest]::Create($Web.Url)
      $request.AllowAutoRedirect = $false
      $request.proxy = [System.Net.WebRequest]::DefaultWebProxy

      $request.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredential
      $request.ContentType = “application/x-www-form-urlencoded”

      $request.Method=”GET”
      $request.UserAgent = “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)”

      $request.GetResponse().StatusCode

      Also notice that you’ll have to add a UserAgent (the one i used there works fine for Win2k8R2, SP2010 SP1 using FBA)..

      All depending on the size of the sitecollection you may end up with a timeout at times – i’m working on a fix for that.

      Comment by Brian H. Madsen — September 22, 2011 @ 5:36 am

  29. Hi All,
    I am trying to setup a PowerShell warm-up script to run within Task Scheduler. The warm up script I am using is found @ http://kirkhofer.wordpress.com/2008/10/18/sharepoint-warm-up-script/ The Warmup.bat and Warmup.ps1 scripts run perfectly when executed manually outside Task Scheduler. When I setup the same Warmup.bat file (that links to Warmup.ps1 file) to run within Task Scheduler it runs with the following messages in the History tab:

    Event ID Task Category Time and Date
    102 Task completed 19/07/2011 12:17:00
    102 Action completed 19/07/2011 12:17:00
    102 Created Task Process 19/07/2011 12:17:00
    102 Action Started 19/07/2011 12:17:00
    102 Task Started 19/07/2011 12:17:00
    102 Task Engine received message to start task 19/07/2011 12:17:00
    102 Task triggered by user 19/07/2011 12:17:00

    As shown above in the time and date column the task completes immediately after it was triggered. This is not normal behaviour. It should take two to three minutes to complete. Also I have setup the Warmup.bat file to generate an output file. Running the scripts manually it creates the output file OK, but when the same script is run within Task Scheduler no output file is created. It’s almost like it is not running the Warmup.bat file. This is strange because I have run setup batch scripts before without any problems.

    I am using the SharePoint Farm account to run the batch file from within Task Scheduler, and the batch file is located locally on the server and not remotely.

    Has anyone come across this problem before?

    I hope you can help.
    CEStar.

    Comment by Warm-up Script not running from within Task Scheduler, But working OK manually — July 19, 2011 @ 12:31 pm

  30. Hi All,

    I am trying to setup a PowerShell warm-up script to run within Task Scheduler. The warm up script I am using is found @ http://kirkhofer.wordpress.com/2008/10/18/sharepoint-warm-up-script/ The Warmup.bat and Warmup.ps1 scripts run perfectly when executed manually outside Task Scheduler. When I setup the same Warmup.bat file (that links to Warmup.ps1 file) to run within Task Scheduler it runs with the following messages in the History tab:

    Event ID Task Category Time and Date
    102 Task completed 19/07/2011 12:17:00
    102 Action completed 19/07/2011 12:17:00
    102 Created Task Process 19/07/2011 12:17:00
    102 Action Started 19/07/2011 12:17:00
    102 Task Started 19/07/2011 12:17:00
    102 Task Engine received message to start task 19/07/2011 12:17:00
    102 Task triggered by user 19/07/2011 12:17:00

    As shown above in the time and date column the task completes immediately after it was triggered. This is not normal behaviour. It should take two to three minutes to complete. Also I have setup the Warmup.bat file to generate an output file. Running the scripts manually it creates the output file OK, but when the same script is run within Task Scheduler no output file is created. It’s almost like it is not running the Warmup.bat file. This is strange because I have run setup batch scripts before without any problems.

    I am using the SharePoint Farm account to run the batch file from within Task Scheduler, and the batch file is located locally on the server and not remotely.

    Has anyone come across this problem before?

    I hope you can help.
    CEStar.

    Comment by Alan Kent — July 19, 2011 @ 12:34 pm

  31. Hi,

    we also created a warmup script and made it available on codeplex. so everyone that is interested in contributing can get more information on our blog: http://sharepointtrench.com/tag/spwarmup/

    Comment by Mario Meir-Huber — August 5, 2011 @ 7:52 pm

  32. Unable to downlaod from the link provided…

    Comment by Chetan — October 5, 2011 @ 5:24 pm

    • Updated. Haven’t touched this in years. Same script as above code though.

      Comment by kirkhofer — October 9, 2011 @ 8:36 pm


RSS feed for comments on this post. TrackBack URI

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 )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.