SharePoint Warm Up with PowerShell

Every SharePoint farm needs a warm up Smile – After giving the IIS 7.5 Application Warmup a chance… I had to do it in another way…

I created a warmup script in PowerShell.

Here it is…

[Update 2011/08/03] New script with “timeout” capability: https://blog.kenaro.com/2011/08/03/sharepoint-warm-up-now-with-timeout/

 

$urls= @("http://sharepoint.local", "http://anothersharepoint.local")

New-EventLog -LogName "Application" -Source "SharePoint Warmup Script" -ErrorAction SilentlyContinue | Out-Null

$urls | % {
    $url = $_
    try {
        $wc = New-Object System.Net.WebClient
        $wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials
        $ret = $wc.DownloadString($url)
        if( $ret.Length -gt 0 ) {
            $s = "Last run successful for url ""$($url)"": $([DateTime]::Now.ToString('yyyy.dd.MM HH:mm:ss'))" 
            $filename=((Split-Path ($MyInvocation.MyCommand.Path))+"lastrunlog.txt")
            if( Test-Path $filename -PathType Leaf ) {
                $c = Get-Content $filename
                $cl = $c -split '`n'
                $s = ((@($s) + $cl) | select -First 200)
            }
            Out-File -InputObject ($s -join "`r`n") -FilePath $filename
        }
    } catch {
          Write-EventLog -Source "SharePoint Warmup Script"  -Category 0 -ComputerName "." -EntryType Error -LogName "Application" `
            -Message "SharePoint Warmup failed for url ""$($url)""." -EventId 1001

        $s = "Last run failed for url ""$($url)"": $([DateTime]::Now.ToString('yyyy.dd.MM HH:mm:ss')) : $($_.Exception.Message)" 
        $filename=((Split-Path ($MyInvocation.MyCommand.Path))+"lastrunlog.txt")
        if( Test-Path $filename -PathType Leaf ) {
          $c = Get-Content $filename
          $cl = $c -split '`n'
          $s = ((@($s) + $cl) | select -First 200)
        }
        Out-File -InputObject ($s -join "`r`n") -FilePath $filename
    }
}

It will write a log file to the same directory as the script itself is stored.

Exceptions will be written to the event log!

Run it as “Scheduled Task” with the farm account credentials. – Check “Run with Highest Priviledges” !!!

Be sure this account can write to the scripts directory (for logging purpose)!

You can use this XML file for quick importing the scheduled task defintion. Therefore the script must be placed in the folder “C:Program FilesSharePoint Warmup”. The script itself must be named “sharepointwarmup.ps1”.

 <?xml version="1.0" encoding="utf-16"?>
 <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
   <RegistrationInfo>
     <Date>2011-01-26T00:00:37.2455395</Date>
     <Author>DOMAIN\ikarstein</Author>
   </RegistrationInfo>
   <Triggers>
     <CalendarTrigger>
       <Repetition>
         <Interval>PT5M</Interval>
         <Duration>P1D</Duration>
         <StopAtDurationEnd>false</StopAtDurationEnd>
       </Repetition>
       <StartBoundary>2011-01-26T00:00:37.2455395</StartBoundary>
       <Enabled>true</Enabled>
       <ScheduleByDay>
         <DaysInterval>1</DaysInterval>
       </ScheduleByDay>
     </CalendarTrigger>
   </Triggers>
   <Principals>
     <Principal id="Author">
       <UserId>DOMAIN\spfarm</UserId>
       <LogonType>Password</LogonType>
       <RunLevel>HighestAvailable</RunLevel>
     </Principal>
   </Principals>
   <Settings>
     <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
     <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
     <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
     <AllowHardTerminate>true</AllowHardTerminate>
     <StartWhenAvailable>false</StartWhenAvailable>
     <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
     <IdleSettings>
       <StopOnIdleEnd>true</StopOnIdleEnd>
       <RestartOnIdle>false</RestartOnIdle>
     </IdleSettings>
     <AllowStartOnDemand>true</AllowStartOnDemand>
     <Enabled>true</Enabled>
     <Hidden>false</Hidden>
     <RunOnlyIfIdle>false</RunOnlyIfIdle>
     <WakeToRun>false</WakeToRun>
     <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
     <Priority>7</Priority>
   </Settings>
   <Actions Context="Author">
     <Exec>
       <Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
       <Arguments>-command ".\SharePointWarmup.ps1"</Arguments>
       <WorkingDirectory>C:\Program Files\SharePoint Warmup</WorkingDirectory>
     </Exec>
   </Actions>
 </Task>
 

1. Copy this XML code to a file, then open the “Scheduled Tasks” management console of Windows.

2. Open the “Task Scheduler Library”, right-click on it’s node. Choose “Import”.

3. Then select the XML file.

4. In the task edit dialog: Change the execution account.

5. Save the task.

6. Now enter the password of the execution account in the credential dialog.

7. Now right-click on the created scheduled task.

8. Choose “Run” to test the task.

9. Have a look into the scripts folder in file system. there must be a file named “lastrunlog.txt” – This file will contain at most 200 lines of loggin informations!

6 thoughts on “SharePoint Warm Up with PowerShell

  1. Excellent, thank you!
    Just one question. My SharePoint Server start slowly, and the scripts logs “The operation has timed out” if it runs after an iisreset. However, the site warmed up, a minute later I browse it right away, so it’s not a big deal, only it make the logfile useless. Is it possible to set a longer timeout period in the script?
    Thanks,
    Tamas

Leave a Reply

Your email address will not be published. Required fields are marked *