Partilhar via


Warm-up script

 

So , you are in need of a warm-up , and do not want to invest in writing and deploying a timer job as per https://msdn.microsoft.com/en-us/library/cc406686(office.12).aspx, or simply you want to keep things to a minimum …

Hmmm.

Here’s a PowerShell script to achieve what you want (the script will issue one request to your site, like you would do through IE):

$url = read-host -Prompt "Enter the URL to get ex https://www.contoso.com/default.aspx"
$wc = new-Object System.Net.WebClient
$httpRequest = [System.Net.HttpWebRequest]::Create($url)
$httpRequest.Credentials =[System.Net.CredentialCache]::DefaultCredentials
$httpRequest.UnsafeAuthenticatedConnectionSharing = "true"
$httpRequest.Method = "GET"
$httpRequest.Timeout = 300000
$objResponseReader = [System.IO.StreamReader]($httpRequest.GetResponse().GetResponseStream())
$httpResponse = $objResponseReader.ReadToEnd().ToString()

save this as WarmUp_One.ps1 and your’re all set. Whenever in need, you can run this and target it to an URL to your liking.

 

Now if you want to warm them all Smile, here’s the modified version for all your web applications.

 

add-pssnapin Microsoft.SharePoint.Powershell
$wc = new-Object System.Net.WebClient

function Warm_UP ([string]$url)
{
write-host("warming up  "+$url +"....")
$httpRequest = [System.Net.HttpWebRequest]::Create($url)
$httpRequest.Credentials =[System.Net.CredentialCache]::DefaultCredentials
$httpRequest.UnsafeAuthenticatedConnectionSharing = "true"
$httpRequest.Method = "GET"
$httpRequest.Timeout = 300000
    try
    {
    $responsecode = $httpRequest.GetResponse()
    $objResponseReader = [System.IO.StreamReader]($httpRequest.GetResponse().GetResponseStream())
    $httpResponse = $objResponseReader.ReadToEnd().ToString()
    Write-Host ($reponsecode.StatusCode)
    }
    catch
    {
     Write-Host("Error")
    }
}

get-spwebapplication | Foreach-Object {Warm_UP($_.Url)}
write-host("all warm and fuzzy inside :)")

Save this as WarmUp_All.ps1 and execute it in PowerShell, or schedule it to run with task manager

 

Technical details on the functions used:

https://technet.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

Comments

  • Anonymous
    January 01, 2003
    Hi Victor, Great script! I've extended this script to work on our environment at work. I have a quick question for you, why are you instantiating both the web client and the httpwebrequest classes? To my understanding the web client and httpwebrequest are very similar and a lot of the scripts that are used for "warm-ups" use one or the other. However, in my environment I require both like you have in your script and was wondering what the process behind that was. Thank you!

  • Anonymous
    January 01, 2003
    There are also some cool Codeplex options for this. sharepointwarmup.codeplex.com is an active and stable utility that integrates with Central Admin for warmups. Personally I've been using http://spwakeup.codeplex.com/ for years even though it's noted as Beta.