Tag Archives: PowerShell Warm Up Script

Warm up your SharePoint Sites with PowerShell

A recommend SharePoint practice is to setup your Web Application Pools to recycle on each WFE on a normal schedule. Most of the time this process takes place in the middle of night and basically restarts the W3P.exe worker process associated to each App Pool. This clears the cache for a web application so not to be bloated. The problem is that when your users come in to work each morning and hit their site, the worker process has to spin up and then serve up the site.  Over the years of working in SharePoint 2007 I have seen many scripts to address this issue. Many of the scripts I have ported to 2010 as well but after reviewing an article on Kirk Hofer’s Blog I have updated my scripts to use PowerShell. Below is my implementation and simple updated ps script.

First Let’s review the schedule for a web application to reset daily.

  • Open IIS Manager and select Application Pools. Next select the application pool in question. Lastly click on “Recycling…” in the right actions area.

image

  • In the application pool’s recycling settings you can view, edit or add reset times.

image

 

Now we will setup a Scheduled Task to execute a PowerShell script daily that warms up all sites on the web front end. Please note that multiple front ends should each have their own scheduled task to warm up each front end. Also please note that I log in to each server using the SP admin service account and ensure that the password is stored to each task for that account. This ensures that the task runs under the credentials of the Admin Service account.

  • First setup the PowerShell script that performs the warm up. the script is fairly simple. The first five lines verify that the SharePoint PowerShell extension has been loaded. Lines 7 through 14 is the function that calls each site url and pulls down the default page content. Lastly line 16 through 19 is a call to get all the site urls in the default zone of the SP Farm. You can apply differing parameters for the method Get-SPAlternateURL (TechNET).
   1: $ver = $host | select version

   2: if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 

   3: if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {

   4:     Add-PSSnapin "Microsoft.SharePoint.PowerShell"

   5: }

   6:  

   7: function Get-WebPage([string]$url)

   8: {

   9:     $wc = new-object net.webclient;

  10:     $wc.credentials = [System.Net.CredentialCache]::DefaultCredentials;

  11:     $pageContents = $wc.DownloadString($url);

  12:     $wc.Dispose();

  13:     return $pageContents;

  14: }

  15:  

  16: Get-SPAlternateUrl -Zone Default | foreach-object {

  17:     write-host $_.IncomingUrl;

  18:     $html = Get-WebPage -url $_.IncomingUrl;

  19: }

  • Next we will setup a Scheduled Task to call the psWarmUp Script. First click on Task Scheduler under Administration Tools. In the Actions area, click on “Create Basic Task”

image

  • Give the task a name and optional description. Click Next.

image

  • Select Daily task to start. Click Next.

image

 

  • Select a start time that is after the time your application pool’s recycle and before everyone shows up for work the next day. Click Next.

image

 

  • Click “Start a Program” and click next. Next we will enter the execution path and paramaters for the powershell file to execute.

image

NOTE:

Program/script:

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -command "psWarmUp.ps1"

Start in:

This is the location of the powershell script. DO NOT USE QUOTES

i.e. C:\Tools\Scripts

  • Click Next and a popup will appear. Please click OK.

image

 

  • Lastly you get to a Summary Screen. you can click the check box to see properties view or edit details on the task.

image

 

That wraps up the setup of the task and PowerShell script to warm up your servers each morning. to ensure everything is working correctly you can immediately run the script and also test over a couple days. To verify the task is running correctly then verify on the history tab of the task.

image