Friday, May 17, 2013

#scsm Self Service Portal Warm up scripts

As I am sure you have noticed by now, sometimes SharePoint needs a little wake-up call if it hasn’t been used in some time. To work around this and ensure the best portal performance for my users, I added all the sites as home page tabs in IE, and opened my browser on a regular basis. But honestly, that’s a schlep, and when I am not in the office, this doesn’t happen.

The US SCSM User Group posted a great script a little while ago that does just that – wake up the SharePoint sites to speed up load time for your users. In their example, they use Windows Scheduled tasks to execute the script on an hourly basis.

I modified the script a little to allow for a new log file daily, and also use Orchestrator to run this script instead.

My runbook looks like this:

image

 

Updated script as follows:

# SharePoint Warmup Script

#region MyWebClient
    Add-Type -ReferencedAssemblies "System.Net" -TypeDefinition @"
    using System.Net;

    public class MyWebClient : WebClient
    {
        private int timeout = 60000;

        public MyWebClient(int timeout)
        {
            this.timeout = timeout;
        }

        public int Timeout
        {
            get
            {
                return timeout;
            }
            set
            {
                timeout = value;
            }
        }

        protected override WebRequest GetWebRequest(System.Uri webUrl)
        {
            WebRequest retVal = base.GetWebRequest(webUrl);
            retVal.Timeout = this.timeout;
            return retVal;
        }
    }
"@
#endregion


$urls= @("enter site url here")

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

$timeout = 60000 #=60 seconds  

$LogFileName = $([DateTime]::Now.ToString('yyyymmdd')) + "_scsm_spp_wakeup.txt"
$LogFilePath = "C:\logs\"


$urls | % {
    $url = $_
    try {
        $wc = New-Object MyWebClient($timeout)       
        $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=(($LogFilePath)+$LogFileName)
            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=(($LogFilePath)+$LogFileName)
        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
    }
}

 

I have highlighted the bits that you will need to update to implement.

Note: if you have copied this script as is here, you may need to first clean up the quotes, etc in Notepad before implementation.

 

And, of course, as it is a Sharepoint script, you can use this for your corporate Sharepoint deployments as well Smile

No comments:

Related Posts with Thumbnails