SharePoint 2010 FBA Powershell Warmup Script [quick and dirty]

I was browsing around for for a quick and dirty SharePoint 2010 warmup script for Powershell the other day, and found quite a few – however, all would fail for me.

See, the environment i’m running is a FBA (or rather AD + ADFS, multiple domains, no trust) environment and the scripts i found would all throw a 403 Forbidden error.

Figured quickly that this was of course caused by the WebClient call that most scripts utilised, so to overcome this obstacle i changed the WebClient call out with a WebRequest call and added the all-important UserAgent (this is the reason you’d get the 403 Forbidden error btw).

So, quick and dirty solution…still some stuff to do on it, such as ensure that the timeout error can be handled – but that’s for another day.

if (( Get-PSSnapin -Name “Microsoft.SharePoint.Powershell” -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.Powershell
}

function WarmupSite() {
param([System.Net.NetworkCredential]$NetworkCred,[Microsoft.SharePoint.SPWeb]$Web)

start-sleep 1

$request = [System.Net.WebRequest]::Create($Web.Url)
$request.AllowAutoRedirect = $false
$request.proxy = [System.Net.WebRequest]::DefaultWebProxy

$request.Credentials = $NetworkCred
$request.ContentType = “application/x-www-form-urlencoded”

$request.Method=”GET”
$request.UserAgent = “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)”

return $request.GetResponse().StatusCode

}

function RecurseSite() {
param([Microsoft.SharePoint.SPWeb]$SiteIdentity)
write-output (“Site: $($SiteIdentity.Url) – $($SiteIdentity.Title)” | format-list)
$cred = [System.Net.CredentialCache]::DefaultNetworkCredentials

if ($SiteIdentity.Webs.Count -gt 0) {
foreach  ($subweb in $SiteIdentity.Webs) {
$output = WarmupSite -NetworkCred $cred -Web $subweb
$output | Out-file -FilePath “C:\isd\output\$($subweb.Title).txt”
RecurseSite -SiteIdenitity $subweb
}
$SiteIdentity.Dispose()
}
else {
$output = WarmupSite -NetworkCred $cred -Web $SiteIdentity
$output | Out-file -FilePath “path to local network folder\$($SiteIdentity.Title).txt”
$SiteIdentity.Dispose()
}
}

$webappservices = (Get-SPFarm).services | ? {$_.typename -eq “Microsoft SharePoint Foundation Web Application” }

foreach ($webapp in $webappservices.WebApplications) {
foreach ($sitecollection in $webapp.Sites) {
write-output “Site collection: $($sitecollection.Url)”
RecurseSite -SiteIdentity $($sitecollection.RootWeb)
}
}

I also added a “start-sleep 1”¬†as SharePoint 2010 would go crazy if i just let it run without it (too many SP objects).

Disclaimer – this code is provided “AS IS”..should be no danger in running it, however, clean it up as it’s a subset of my own code here (doing other things too) and is a cut n’ paste n’ edit.