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!

Start-up and Tear-down SharePoint scripts

As part of my daily SharePoint activities I typically will be starting and stopping my locally installed instance of  SharePoint a number of times on my development laptop throughout the day. I’m lucky enough to have a Dell M6500 with 8 cores and 16GB RAM (running Windows 7) but even so, when I’m running VMware and have my full 6 server development farm running I need all the RAM I can get.

Start-up

To help me quickly start-up my local SharePoint I use the following script:

color 2f
Echo Starting SharePoint + other services…

sc stop WSearch

sc start IISADMIN
sc start W3SVC

sc start SQLWriter
sc start SQLBrowser
sc start MSSQLSERVER
sc start MSSQLServerOLAPService
sc start MSOLAP$POWERPIVOT
sc start ReportServer

sc start SPAdminV4
sc start SPTimerV4
sc start SPTraceV4
sc start SPSearch4
sc start OSearch14

sc start WebAnalyticsService
sc start hMailServer

Echo Set Service Start Options to manual

sc config IISADMIN start= demand
sc config W3SVC start= demand
sc config WebAnalyticsService start= demand
sc config SQLBrowser start= demand
sc config MSSQLSERVER start= demand
sc config MSSQLServerOLAPService start= demand
sc config MSOLAP$POWERPIVOT start= demand
sc config ReportServer start= demand
sc config SQLWriter start= demand
sc config SPAdminV4 start= demand
sc config SPTraceV4 start= demand
sc config SPTimerV4 start= demand
sc config OSearch14 start= demand
sc config SPSearch4 start= demand
sc config hMailServer start= demand

Echo Set Service Start Options to disabled

sc config SPUserCodeV4 start= disabled
sc config SPWriterV4 start= disabled
sc config DCLauncher14 start= disabled
sc config DCLoadBalancer14 start= disabled

Echo Starting IIS

iisreset /start

Echo Starting other apps

start /d "C:\Program Files (x86)\Sans" /min sans.exe

Echo Launching browser

start "C:\Users\Brian\AppData\Local\Google\Chrome\Application\chrome.exe" http://bc01

Echo Starting powershell scripts

powershell -File "C:\Documents\SharePoint\Demo\Create Local Users\Update Status.ps1"

Echo SharePoint is ready for duty!

honk.exe "C:\documents\sharepoint\sharepointpodshowtheme.wav"

You’ll notice this does a few things, but I think the important one to highlight are (apart from starting SharePoint) it sets all the required and dependant services start-up option to be ‘manual’. This ensure that once I’ve rebooted my machine, SQL and SharePoint remain stopped until I need them. Additionally, as I’m demonstrating with SharePoint a fair amount it will call my random user profile update PowerShell script to simulate some activity into the user activity feeds, more details of this script can be found here: https://sharepintblog.com/2011/07/01/randomly-update-user-profiles-from-powershell-what-the-heck/

Finally, once my SharePoint is started and warmed up, the script plays the awesome theme to the SharePoint Pod Show courtesy of Rob Foster (http://www.robfoster.net/) to let me know I’m ready to roll. If you haven’t discovered the SharePoint Pod Show check out http://www.sharepointpodshow.com/ for some great content. Someone really needs to start a Facebook campaign to save the theme tune for this show as there are rumours it might be changed – sacrilege!

Tear-down

Following a similar pattern is my tear-down script which should be fairly self explanatory:

color 4f

powershell -File "C:\Documents\SharePoint\Demo\Create Local Users\Update Status.ps1"

Echo Stopping the services…

sc stop SPAdminV4
sc stop SPTimerV4
sc stop SPTraceV4
sc stop SPSearch4
sc stop OSearch14
sc stop WebAnalyticsService

sc stop ReportServer
sc stop MSOLAP$POWERPIVOT
sc stop MSSQLServerOLAPService
sc stop SQLBrowser
REM sc stop MsDtsServer100
sc stop MsDtsServer110
sc stop SQLSERVERAGENT
sc stop MSSQLSERVER
sc stop SQLBrowser
sc stop SQLWriter
sc stop W3SVC
sc stop IISADMIN
sc stop hMailServer
sc stop SQLBrowser

Echo Set Service Start Options to manual

sc config IISADMIN start= demand
sc config W3SVC start= demand
sc config WebAnalyticsService start= demand
sc config SQLBrowser start= demand
REM sc config MsDtsServer100 start= demand
sc config MsDtsServer110 start= demand
sc config SQLWriter start= demand
sc config SQLBrowser start= demand
sc config SQLSERVERAGENT start= demand
sc config MSSQLSERVER start= demand
sc config MSSQLServerOLAPService start= demand
sc config MSOLAP$POWERPIVOT start= demand
sc config ReportServer start= demand
sc config SQLWriter start= demand
sc config SPAdminV4 start= demand
sc config SPTraceV4 start= demand
sc config SPTimerV4 start= demand
sc config SPSearch4 start= demand
sc config OSearch14 start= demand
sc config hMailServer start= demand

Echo Set Service Start Options to manual

sc config SPUserCodeV4 start= disabled
sc config SPWriterV4 start= disabled
sc config DCLauncher14 start= disabled
sc config DCLoadBalancer14 start= disabled

tskill sans /A

tskill dw20 /A
tskill iTunesHelper /A
tskill groove /A
tskill onenotem /A
tskill smax4pnp /A
tskill wfcrun32 /A
tskill msosync /A
tskill concentr /A
tskill SPPSVC /A
tskill c2wts /A
tskill c2WTShost /A

iisreset /stop

Enjoy!