Warm up a SharePoint Web Application with PowerShell

Here’s my script for warming up a SharePoint Web Application with PowerShell. It should be fairly self explanatory but note the following:

  • It uses System.Net.HttpWebRequest so does not require access to the SharePoint PowerShell cmdlets or object model – in fact this could work with most web based applications.
  • It uses the credentials from the current context so the process running the script must be able to access the url and authenticate automatically – typically via the IE Local Intranet zone.

Script:

function Get-HTTPRequest {
   param ( $URL = (Read-Host "Enter the URL of the page to request") )
  
   ## Create our request object
   $Request = [System.Net.HttpWebRequest]::Create($URL);
  
   ## Use the default (current) credentials
   $Request.UseDefaultCredentials = $true
  
   try
   {

        ## Make the request
        $Response = $Request.GetResponse();
       
        ## Process the response
        if($Response.StatusCode -eq 200) {
       
            ## Get the length of the response
            [int]$Length = $Response.ContentLength
           
            ## Get the response stream
            $Stream = $Response.GetResponseStream()

            ## Create a byte array so we can read our buffer in chunks
            [byte[]]$Chunk = new-object byte[] 1024
           
            ## Create a running total of our progress
            [int]$TotalBytesRead = 0
            [int]$BytesRead = 0
           
            ## Loop through the stream reading a Chunk (1024 bytes) at a time
            do {
                ## Read a Chunk
                 $BytesRead = $Stream.Read($Chunk, 0, $Chunk.Length)
               
                ## Accumilate the total bytes read
                $TotalBytesRead += $BytesRead
               
                ## Determine if there is anything left on the response stream to read
                if($Length -gt 0)
                {
                    ## Update progress
                       Write-Progress "GET $url" "Bytes read: $TotalBytesRead of $Length" -percentComplete (($TotalBytesRead/$Length)*100)
                }
                else
                {
                    ## Nothing left to read – we are all done.
                       Write-Progress "GET $url" "Total bytes read: $TotalBytesRead bytes"
                }
            }
            ## Loop until there are no more bytes to read
            while ($BytesRead -gt 0)

            ## Close the response stream
            $Stream.Close()
        }

        ## Close the response
        $Response.Close();
   }
   catch
   {
           ## Ooops
        Write-Host "ERROR: $_.Message" -ForegroundColor Red
        return
   }
 
}

Get-HTTPRequest "http://bc01/SitePages/Home.aspx"
Get-HTTPRequest "http://bc01/Search/Pages/default.aspx"
Get-HTTPRequest "http://bc01/_layouts/viewlsts.aspx"
Get-HTTPRequest "http://bc01/my/default.aspx"
Get-HTTPRequest "http://bc01/my/person.aspx"
Get-HTTPRequest "http://bc01/_vti_bin/socialdataservice.asmx"
Get-HTTPRequest "http://bc01/_vti_bin/ExcelRest.aspx/Documents/Book1.xlsx/Model"
Get-HTTPRequest "http://bc01/_vti_bin/ExcelRest.aspx/Documents/Book1.xlsx/Model/Charts(‘Chart%201’)?$format=image"
Get-HTTPRequest "http://bc01/Search/Pages/results.aspx?k=SharePoint"
Get-HTTPRequest http://bc01/Pages/Mashup-Demo1.aspx

You’ll notice the script contains a single function called Get-HTTPRequest that reads the response from each URL passed into it and displays progress via the Write-Progress command like this:

image

Feel free to re-use this as you see fit. Enjoy!

Advertisements

One Response to Warm up a SharePoint Web Application with PowerShell

  1. Amancio says:

    Thnx needed this for speeding up the search!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: