Configure SharePoint Blob Cache using PowerShell


I am working on an installation of SharePoint 2010 For Internet Sites for my present client, and I’m automating almost everything using PowerShell cmdlets, scripts and advanced functions.
A lot of credit goes to guys like Phil Childs and Brian Lalancette for their work around PowerShell, however; today I’m thanking Ed Wilson, Scripting Guy for his post on Using PowerShell to script changes to the SharePoint Web.config file.
Thanks to this post, I was able to adapt with VERY little work to my needs. Really the only thing I needed that wasn’t provided in this set of functions was the ability to use a different location (not the default C:\BlobCache\14).
It was very easy to add another $configMod section ($configMod3) to do this, here’s the additional code I added:
I also added a parameter for $BlobCacheLocation, so you can provide the WebApplication and BlobCacheLocation values from the pipeline.
Easy, efficient and clean!
Here’s the code I added:
$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
$WebApp.WebConfigModifications.Add( $configMod3 )

Here’s the entire set of functions:

# 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="C:\BlobCache\14"
) 

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() 
} 
}
Advertisement

4 thoughts on “Configure SharePoint Blob Cache using PowerShell

  1. Alvira Acosta

    Well PowerShell is an option but in this modern age where we have a lot of third party tools by using which we can do such things in very minimal time then why to use PowerShell or any other manual procedures.

  2. Ryan Dennis

    Thanks Alvira,
    I’m sure there are plenty of third party tools out there to accomplish this and many other tasks which I’ve blogged about. Frankly if I wanted to blog about third party tools I could post daily and still have plenty of content. Please don’t use my blog as a forum for free sales and advertising. I blog about things which administrators and developers can do without purchasing third party tools – and as a SharePoint consultant, many of my clients don’t want or don’t have the means to purchase third party everytime they have a need. PowerShell and other things give them ways to accomplish tasks without a. purchasing additional software and b. installing additional software.

    Ryan

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s