While diving deeper into the SharePoint 2010 PowerShell command-lets I realised that my last blog using a borrowed script based on 2007 was in need of a major update for 2010, so see here a version doing the same thing but with no use of STSADM (and thus no need to run as admin in 2008).
Warmup2.ps1
############################################################################
#WarmUp2.ps1 - Enumerates all web sites in web applications in a 2010
# SharePoint farm and opens each in a browser.
#Notes:
#-"get-webpage" function borrowed from:
# http://kirkhofer.wordpress.com/2008/10/18/sharepoint-warm-up-script/
#
#Assumptions:
#-Running on machine with WSS/MOSS 2010 installed
############################################################################
Add-PsSnapin Microsoft.SharePoint.PowerShell
$extrasitelistfile = 'c:ToolsWarmupwarmup-extrasites.txt'
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")
$apps = get-spwebapplication -includecentraladministration
foreach ($app in $apps) {
$sites = get-spsite -webapplication $app.url
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;
}
}
The script still retrieves a list of all sites in all web applications in the farm (including Central Admin) and additionally retrives a list of sites in a text file by default located in “C:ToolsWarmupwarmup-extrasites.txt”
Warmup-extrasites.txt
http://servername/ReportServer
Enjoy!
When executing the script, I receive a “The path is not of a legal form” message for a subsite. See below:
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:Usersspfarmsvc> d:
PS D:> cd scripts
PS D:scripts> ./warmup.ps1
http://einsite
http://einsite/sites/projectsharepoint
Exception calling “DownloadString” with “1” argument(s): “The path is not of a legal form.”
At D:scriptswarmup.ps1:23 char:28
+ return $wc.DownloadString <<<
Check your account permissions, I noticed the error returned if you don’t have permissions to the site in question is something similarly unhelpful.
Also from the output you can see that it is successfully opening the first URL ‘http://einsite’ so I imagine it is something specific with that second one in the list.
Great script. Thanks. Working perfectly.
Hi there,
I got the following error:
Exception calling “DownloadString” with “1” argument(s): “The remote server returned an error: (401) Unauthorized.”
where can I check my account permission?
Hi Dex,
Permissions for most SharePoint sites are under Site Settings / Site Permissions, if the user account running the script does not have some permission to the site then it will fail.
HTH,
Hi Martin,
Does this script work with forms based authenticated sites? Also we are using host headers. When I try to run the script from PowerShell I’m getting the 401, 403 and 500 errors (see below -even though I tried running the script with Farm admin account) – and no TXT file created either.
I saw users with FBA and host headers complains in kirkhofer’s blog as well. Do you know whether there is any fix to this?
Many thanks for looking into this.
Exception calling “DownloadString” with “1” argument(s): “The remote server ret
urned an error: (401) Unauthorized.”
At D:DeployedWarmupWarmup.ps1:23 char:28
+ return $wc.DownloadString <<<< ($url);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
===================
Exception calling "DownloadString" with "1" argument(s): "The remote server ret
urned an error: (403) Forbidden."
At D:DeployedWarmupWarmup.ps1:23 char:28
+ return $wc.DownloadString <<<< ($url);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
===================
Exception calling "DownloadString" with "1" argument(s): "The remote server ret
urned an error: (500) Internal Server Error."
At D:DeployedWarmupWarmup.ps1:23 char:28
+ return $wc.DownloadString <<<< ($url);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Hi Blue,
Firstly no, this script does not work for Forms based sites. I have created another one for that which deserves a blog of it’s own (it’s 100x more complex than this one), here is a useful MSDN article which lead me to that solution: http://msdn.microsoft.com/en-us/magazine/cc507642.aspx as you can see you have to do the html login manually and save and use the auth-cookie, not nice.
Otherwise with your issues, logged firstly the 401 I would guess is permissions related. You shouldn’t need to “run as” but either way the account logging in should have permissions in Site Settings (or if using a service account this can be done in the Central Admin – Site Administration).
I have just tried this on one of my lab servers with 20+ web apps, all but one of which use host headers, so what that showed me is that it does work but not 100% of the time.
What I would suggest checking is:
– Firstly that your account running the script can download those pages found in IE on the server.
– That the server can be found, ie any required DNS / Hosts file updates are done (if using an external FQDN this is probably required)
– That the sites work! :) In my lab of those 30 odd sites half of them had broken webparts and the like causing some of the 500 errors!
Hope that helps,
Martin.
Just to say it works great for me – just what I needed. Many thanks
Hi Martin,
Like the script! Question, is it possible to exclude certain sites? Meaning, I don’t want the script to warm up all the mysites (http://mysite.mydomain.com/…)
Thanks
Hi there!
If you add something like:
if($app.url -notlike ‘http://mysite*’) {
write-host … [from the scripte]
}
Into the main foreach loop!
HTH,
Thanks. That works.
Hi there
The script works fine for me – except for Project Server 2010 sites (PWA). Then I get the error:
Exception calling “DownloadString” with “1” argument(s): “The remote server ret
urned an error: (500) Internal Server Error.”
At D:DeployedWarmupWarmup.ps1:23 char:28
+ return $wc.DownloadString <<<< ($url);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
All Native SharePoint sites works fine! Any ideas?
Br, Lars
Hi,
on some sites i am getting following error. even i run script through farmadmin account.
Exception calling “DownloadString” with “1” argument(s): “The operation has tim
ed out”
At D:irfan workingscriptsWarmup2.ps1:23 char:28
+ return $wc.DownloadString <<<< ($url);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
pls tell how to fix
Great script. Thanks for posting it.
I added -LIMIT ALL to the Get-SPSite command because some webapps have more than 200 site collections.
“Limits the maximum number of site collections to return. The default value is 200.
The type must be a valid non-negative number. Specify ALL to return all site collections for the given scope.”
http://technet.microsoft.com/en-us/library/ff607950.aspx
Hi,
i have 1 APP Server and 1 Web Server i run script on Web Server giving farm admin credential and web server host name. i have site with host name like http://myportal.mycompany.com
When i run script getting following error message. pls tell how to fix it.
Exception calling “DownloadString” with “1” argument(s): “The remote server ret
urned an error: (404) Not Found.”
At C:SPWorkingWarmUp_ScriptWarmup2.ps1:9 char:28
+ return $wc.DownloadString <<<< ($url);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "DownloadString" with "1" argument(s): "The operation has tim
ed out"
At C:SPWorkingWarmUp_ScriptWarmup2.ps1:9 char:28
+ return $wc.DownloadString <<<< ($url);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Works like a dream. Thank you for sharing this, I also added in a piece to write an informational event to the application event log when it completes at the end of the script.