Friday, October 31, 2014

SharePoint 2013 Warmup Script

Context: People were complaining over the performance of their SharePoint sites. So, I decided to write a SharePoint 2013 warmup script. I followed Jasonth script and changed it according to my needs by adding a xml file where I specified all the configurations. The configuration file consist of information, which webapplications should be invoked fully (all Site collections) and which specific site collections should be invoked.
The following is my PowerShell warmup script for Sharepoint 2013;
clear

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$configurationFilePath = 'D:\Workspace\PS-Scripts\WarmupSettings.xml';

Start-SPAssignment -Global

if (test-path $configurationFilePath) {
    
    [xml]$configurationFile = Get-Content $configurationFilePath;

    Write-Host "Invoking WebApplications with all site(s)...." -ForegroundColor Cyan;
    # Get All WebApplications whose all sites will be invoked
    $appUrls = Select-Xml -Xml $configurationFile -XPath "//WebAplication[@AllSites='TRUE']" |
                    ForEach-Object { $_.Node.url }
                              
    foreach ($appUrl in $appUrls) {        
        $sites = get-spsite -webapplication $appUrl -Limit ALL
        write-host 'WebApplication: ' $appUrl -foregroundcolor "magenta";
        foreach ($site in $sites) {
            write-host $site.Url;
            $r = Invoke-WebRequest -URI $site.Url -UseDefaultCredentials
            $r.StatusCode
        }        
    }

    Write-Host "Invoking Specific Site Collections......." -ForegroundColor Cyan;
    # Invoke Specific Site Collections
    $apps = Select-Xml -Xml $configurationFile -XPath "//WebAplication[@AllSites='FALSE']";
    foreach ($app in $apps) {
         <#$appUrl = $app.Node.url;
         $app = $apps.Node[0]
         $siteUrls = Select-Xml -Xml $app -XPath "/SiteCollection" |
                     ForEach-Object { $_.Node.InnerText }#>         
         
         $appUrl = $app.Node.url;
         write-host 'WebApplication: ' $app.Node.url -foregroundcolor "magenta";
         foreach($siteUrl in $app.Node.SiteCollection){
            $siteCollectionUrl = $appUrl + $siteUrl
            Write-Host $siteCollectionUrl
            $r = Invoke-WebRequest -URI $siteCollectionUrl -UseDefaultCredentials
            $r.StatusCode
         }                                       
    }

                    
 }

Stop-SPAssignment -Global

Write-Host "Finished Invoking Sites....." -ForegroundColor DarkYellow;
The xml file named as ‘WarmupSettings.xml’ is as follows. It is important to note that each WebApplication element has a property ‘AllSites’. When AllSites is set to TRUE, the PS script is going to invoke each site collection once. When AllSites element is set to false, PS Script will invoke site collections which are specified in the SiteCollection element.

  
 
 
 
  /sites/Help  
 
 
  /sites/test1
  /sites/test2
  /sites/test3
  
   

No comments:

Post a Comment