I needed to enable / configure BLOB caching on multiple sharepoint sites.
This is done by editing the web.config of each SharePoint site, on each SharePoint server.
I wrote this down and dirty script so I would not need to edit all the web.config’s by hand (I had about 20 web.configs to touch)
Note that since its just editing the web.config, we don’t need to run this in a SharePoint shell – I ran it from an ordinary PowerShell command prompt on my workstation.
The script:
Echo "Run this script under an admin account with rights to the servers being hit"
$dir = "\Serverc$inetpubwwwrootwssVirtualDirectories"
$currentDate = (get-date).tostring("mm_dd_yyyy-hh_mm_ss")
# loop through each subdirectory to find each sharepoint site.
foreach ($subdir in dir $dir)
{
# Here In my case, all my SharePoint sites had mydomain.com as part of the folder names,
# So the contains statement was an easy way to only touch the web.config's of actual SharePoint sites
# while leaving alone central admin and other non-SharePoint websites IIS had.
if($subdir.Name.Contains("mydomain.com"))
{
$path = $dir + "" + $subdir.name + "Web.config"
echo $path
$backup = $path + "_$currentDate.bak"
$xml = New-Object XML
$xml.Load($path)
$xml.Save($backup)
$element = $xml.configuration.SharePoint.BlobCache
echo $element
$element.location = "X:BlobCache14"
$element.enabled = "true"
$element.maxSize = "5"
echo $element
$xml.Save($path)
}
}
Over on my old Blog, Basementjack.com, Remco de Groot commented that this isn’t the best way to change a web.config in SharePoint and he’s absolutely right!
He sent over a script from
http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/14/use-powershell-to-script-changes-to-the-sharepoint-web-config-file.aspx
The article above does a great job of explaining the new script and is a must read. In summary, it uses SharePoint to make the change, and this is a huge help down the line if you ever need to add or replace a SharePoint server in the farm.
I’ll leave the original post’s code above for search engines and as an example of using XML with PowerShell. That said, if you’re here to enable Blob Cache, then definately head on over to the article above