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

13 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


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.