SharePoint Warm Up Script

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

Advertisements

49 thoughts on “SharePoint Warm Up Script

  1. Pingback: Simple SharePoint Warm Up Script - Kirk Hofer

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

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

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

    Kirk

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

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

    Roland

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

    Roland

  8. Pingback: SharePoint Powershell Warm Up Script « Roland Serman’s Blog

  9. Pingback: SharePoint using a Warm Up Script « PANVEGAs Blog

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

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

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

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

  13. Pingback: SharePoint PowerShell Warm-Up Script « Harish Mathanan

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

    How can I get round this error?

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

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

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

  18. Pingback: Project Server Blog » Warmup that farm

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

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

    • 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

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

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

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

    • 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

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

  23. Pingback: Warm Up Ergänzungen | SharePoint 2010

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

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

  25. 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;
    }

  26. Pingback: SharePoint Slow Load (SSL) issue « FYano's Tech Blog

  27. Pingback: Easy SharePoint 2010 warmup Script using PowerShell | JonTheNerd

  28. 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();
    }

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

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

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

  31. 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 @ https://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.

  32. 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 @ https://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.

  33. Unable to downlaod from the link provided…

  34. Pingback: Warm up your SharePoint Sites with PowerShell « Sharepoint Nectar

  35. Pingback: PowerShell: An administrator’s best friend #SPSTC recap

  36. Pingback: SP Warm-up « RaSor's Tech Blog

  37. How would you get this script to work for IIS set to SSL Required which requires a user to use their certificate in order to access the URL?

  38. Pingback: Warm up scripts resolves slow loading site -

Comments are closed.