Friday, August 22, 2014

SharePoint 2013 Warmup Scripts using PowerShell

What is "Warmup Scripts" in SharePoint?
By default IIS applications pools recycles every night to keep clean memory space -  Every time during IIS Application pool recycles - Asp.net assemblies to be re-compiled to serve the page to end user. So, When the recycling occurs during mid night (or when you do manual recycling/IISReset), The very first user types the URL, experiences the wait time of 30 seconds to 120 seconds on an average. (again, this depends on your hardware-software configurations!). But the subsequent requests come faster from the server for the same site!

So, the idea is: "warm up" the site before users start requesting it so that they don't suffer at first time hit.  Warm up scripts triggers  requests to your servers regularly to "Warmup" IIS.

Ok, Where is the script? which warm up script I've to use? Well, There are lot many available. Here is one among them: https://spbestwarmup.codeplex.com/ and You may have to customize it based on your requirement.

In my experience, This simple warmup script works amazing with all versions of SharePoint: SharePoint 2013, SharePoint 2010 and in SharePoint 2007:

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

function Get-WebPage([string]$url,[System.Net.NetworkCredential]$cred=$null) 
{ 
    $WebClient = new-object net.webclient 
    if($cred -eq $null) 
    { 
        $cred = [System.Net.CredentialCache]::DefaultCredentials; 
    } 
    $WebClient.credentials = $cred; 
    return $WebClient.DownloadString($url); 
} # end Function 

# Make sure the account has enough permission to read sites
$cred = [System.Net.CredentialCache]::DefaultCredentials; 
#If you need to use an another account
#$cred = new-object System.Net.NetworkCredential("USER ID","PASSWORD","DOMAIN")

#Get All Web Applications and iterate through
$WebAppsColl = Get-SPWebApplication -IncludeCentralAdministration

foreach($WebApp in $WebAppsColl)
{
    #Get All site collections and iterate through
    $SitesColl = $WebApp.Sites

    foreach ($Site in $SitesColl) 
    {
 Write-Host "Warming up Site collection:"  $Site.URL
 $html = Get-WebPage -url $Site.URL  -cred $cred 
    }

#Change these URL's if you need any other Site/URL for warmup
#$html = Get-WebPage -url "http://northwind.crescent.com/SitePages/Home.aspx" -cred $cred 
}
You'll have to add each of your web application to the above list of URLs. For Search web application, You'll have to send a Search query, such as: https://intranet.contoso.com/_layouts/OSSSearchResults.aspx?k=warmup

You have to edit your host files, so that Your Warmup script hits the same WFE server its running, instead of going to the load balancer and gets pages from any other web server.

Warmup Script for Host-Named Site Collections:
Lets add bit more enhancement to it:
  • Lets keep a log for warmed up sites, 
  • Warm-up all Host-named site collections - Lets create a proxy to hit on the SAME web front end rather going to load balancer - We can't edit HOST file for each HNSC, isn't it?
start-transcript -path C:\Scripts\$(get-date -format 'ddMMyyyy').txt -append

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue  

function Get-WebPage([string]$url)
{   
    $bypassonlocal = $false
    $proxyuri = "http://" + $env:COMPUTERNAME
    $proxy = New-Object system.Net.WebProxy($proxyuri, $bypassonlocal)
    $wc = new-object net.webclient
    $wc.credentials = [System.Net.CredentialCache]::DefaultCredentials
    $wc.proxy = $proxy
    $pageContents = $wc.DownloadString($url)
    #write-host $wc.ResponseHeaders.Get("WFE")
    $wc.Dispose()
    return $pageContents
}

#Central Administration
write-host "Warming up Central Administration..."
$WebApps = Get-SPWebApplication -IncludeCentralAdministration
Get-SPWebApplication -IncludeCentralAdministration | ? {$_.IsAdministrationWebApplication -eq $true} | % { $Req = Get-WebPage $_.url }
write-host "`nCentral Administration Warmed up!"

# Warm up Host Name Site Collections (HNSC)
 Write-Host "Warming up Host Name Site Collections (HNSC)...`n"
 $hnsc = Get-SPSite -Limit All |? {$_.HostHeaderIsSiteName -eq $true} | Select Url
 foreach ($sc in $hnsc) {
        write-host "Processing HNSC: "$sc.Url
  $Req = Get-WebPage $sc.url  
 }

# Clean Temporary Files
 Remove-item "$env:systemroot\system32\config\systemprofile\appdata\local\microsoft\Windows\temporary internet files\content.ie5\*.*" -Recurse -ErrorAction SilentlyContinue
 Remove-item "$env:systemroot\syswow64\config\systemprofile\appdata\local\microsoft\Windows\temporary internet files\content.ie5\*.*" -Recurse -ErrorAction SilentlyContinue

stop-transcript

When and How often we've to run Warm up Scripts?
Usually, We schedule it to run before the beginning of the day. Its a good idea to schedule the PowerShell script via Task scheduler and Warmup script must be scheduled on all WFEs of your SharePoint Farm.

Here is how you can schedule PowerShell scripts using Windows task scheduler: Create a Scheduled Task for PowerShell Script with Windows Task Scheduler
Use "Search Crawl Account"to run warm-up script (Provided, This account has "Login as a Batch Job rights!").

You can create a scheduled task with command line:
schtasks /create /tn "Warmup Script" /ru <<Domain\Account>> /rp <<Password>> /rl highest /sc daily /st 01:00 /ri 60 /du 24:00 /tr "PowerShell.exe -ExecutionPolicy Bypass D:\Scripts\Warmup.ps1"  



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

1 comment :

  1. Thanks for the link back to http://spbestwarmup.codeplex.com/

    Glad you like it! If you have any suggestions to improve please drop a line. -SPJeff

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...