Friday, March 22, 2013

How to Warm up SharePoint 2010 Sites using PowerShell


Warming up SharePoint 2010 using PowerShell


 If you’ve been around SharePoint 2007, SharePoint 2010 or just .NET for any amount of time, you know that pages are not compiled until the first time a server request is made to view them. This causes the first load to be the slowest.
There have been numerous solutions for this, most of them are some form of a warm-up script.
In SharePoint 2007, there were a few versions; all of which used the command-line STSADM tool to basically enumerate all sites in a web application and then submit an HTTP request to cache the pages on the server side.
In SharePoint 2010, the STSADM based scripts still work; but why use them? We have PowerShell!
I found some really good examples of PowerShell based Warm-up scripts, but none of them served my purposes or my clients. Since I’m really starting to dig PowerShell, I decided to do my part in the SharePoint community by developing my own version of the SPWarmup script.
-->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#################################################################################
## SPWarmUp.ps1 - Enumerates all web sites in web applications in a SharePoint  #
## 2010 farm and opens each in a browser.  This version does not use STSADM #
## and instead uses PowerShell.                         #
##                                      #
## Notes:                                   #
## -"get-webpage" function borrowed from:                   #
##                                      #
## Assumptions:                                 #
## -Running on machine with SharePoint 2010 installed               #
## Although bits and pieces were borrowed from Kirk Hofer and Martin Laukkanen, #
## The final working version with nice enhancements is courtesy of Ryan Dennis. #
## www.iccblogs.com/blogs/rdennis - Twitter: @SharePointRyan            #
#################################################################################
   
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
# By uncommenting the following lines as well as lines 54-60 you can warmup other sites such as SSRS #
#$extrasitelistfile = 'c:\warmup-extrasites.txt'
Start-SPAssignment -Global 
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
#credentials you can pass them in to elevate 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")
   
$apps = Get-SPWebApplication -IncludeCentralAdministration
foreach ($app in $apps) {
$sites = Get-SPSite -WebApplication $app.url -Limit ALL -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
foreach ($site in $sites) {
Write-Host $site.Url;
$html=get-webpage -url $site.Url -cred $cred;
}
}
## Warm up other sites specified in warmup-extrasites.txt file (such as SSRS)
#if (test-path $extrasitelistfile) {
#   $extrasites = get-content $extrasitelistfile
#   foreach ($site in $extrasites) {
#     write-host $site;
#     $html=get-webpage -url $site -cred $cred;
#   }
#}
Stop-SPAssignment -Global

No comments:

Post a Comment