Powershell Script to enable/disable Blob Caching on SharePoint 2010 Farm


# Enable-SPBlobCache Function            
function Enable-SPBlobCache {             
param(             
                [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]            
                [Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]             
                $WebApplication,            
                [Parameter(Mandatory=$false, ValueFromPipeline=$true, Position=1)]            
                $BlobCacheLocation="E:\BlobCache\"            
)             
            
process {             
    $WebApp = $WebApplication.Read()             
    # SPWebConfigModification to enable BlobCache             
    $configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification             
    $configMod1.Path = "configuration/SharePoint/BlobCache"             
    $configMod1.Name = "enabled"             
    $configMod1.Sequence = 0             
    $configMod1.Owner = "BlobCacheMod"             
    ## SPWebConfigModificationType.EnsureChildNode -> 0             
    ## SPWebConfigModificationType.EnsureAttribute -> 1             
    ## SPWebConfigModificationType.EnsureSection -> 2             
    $configMod1.Type = 1             
    $configMod1.Value = "true"            
                                            
    ######################################################################            
                            
                # SPWebConfigModification to enable client-side Blob caching (max-age)             
    $configMod2 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification             
    $configMod2.Path = "configuration/SharePoint/BlobCache"             
    $configMod2.Name = "max-age"             
    $configMod2.Sequence = 0             
    $configMod2.Owner = "BlobCacheMod"             
    ## SPWebConfigModificationType.EnsureChildNode -> 0             
    ## SPWebConfigModificationType.EnsureAttribute -> 1             
    ## SPWebConfigModificationType.EnsureSection -> 2             
    $configMod2.Type = 1             
    $configMod2.Value = "86400"             
                            
                ######################################################################            
                            
                # SPWebConfigModification to change the default location for the Blob Cache files            
                $configMod3 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification            
                $configMod3.Path = "configuration/SharePoint/BlobCache"            
                $configMod3.Name = "location"            
                $configMod3.Sequence = "0"            
                $configMod3.Owner = "BlobCacheMod"            
                ## SPWebConfigModificationType.EnsureChildNode -> 0             
    ## SPWebConfigModificationType.EnsureAttribute -> 1             
    ## SPWebConfigModificationType.EnsureSection -> 2             
                $configMod3.Type = 1            
                $configMod3.Value = $BlobCacheLocation            
    # Add mods, update, and apply             
    $WebApp.WebConfigModifications.Add( $configMod1 )             
    $WebApp.WebConfigModifications.Add( $configMod2 )            
                $WebApp.WebConfigModifications.Add( $configMod3 )            
    $WebApp.Update()             
    $WebApp.Parent.ApplyWebConfigModifications()             
}             
}            
            
            
            
# Disable-SPBlobCache Function            
function Disable-SPBlobCache {             
param(             
    [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]             
    [Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]             
    $WebApplication             
)             
            
process {             
    $WebApp = $WebApplication.Read()             
    $mods = @()             
    foreach ( $mod in $WebApp.WebConfigModifications ) {             
        if ( $mod.Owner -eq "BlobCacheMod" ) {             
            $mods += $mod             
        }             
}             
    foreach ( $mod in $mods ) {             
        [void] $WebApp.WebConfigModifications.Remove( $mod )             
    }             
    $WebApp.Update()             
    $WebApp.Parent.ApplyWebConfigModifications()             
}             
}            

Working of the Script:

  1. Copy Blobcache.ps1 to local drive on Sharepoint server or on a file share
  2. This script is using Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind  class of Microsoft.SharePoint.PowerShell assembly so we need to make sure we add the reference in PS by running command

Add-PSSnapin Microsoft.SharePoint.PowerShell

  1. Set the executionpolicy to unrestricted
  2. Run the script (Dot source it) – type a period and a blank space and then type the path to the script file (see the screenshot)
  3. Execute Enable-SPblobcache function to enable blobcaching, supply the webapplication name on the prompt
  4. To disable execute Disable-SPblobcache

**Location of Blobcache folder, max age etc can be changed from the script itself.

** If you enable blob caching using Enable-SPblobcache function then be sure to disable it using Disable-SPblobcache function (dont do it manually).

Advertisements