Tuesday, July 23, 2013

SharePoint Object Cache Powershell

This Powershell Script by Chris O'Brien which sets the object caching in SharePoint:

In all installations of SharePoint, it’s important to properly configure object caching so that SharePoint can efficiently retrieve cached versions of items where possible. Technet is clear that SharePoint cannot perform optimally without this being done. 

Object caching configuration mainly involves the configuration of two specific user accounts which SharePoint will use to simulate a reader and high-privileged user. If these accounts are not configured, you’ll see entries like this in the Windows event log:

Object Cache: The super user account utilized by the cache is not configured. This can increase the number of cache misses, which causes the page requests to consume unneccesary system resources.

The full details of why this configuration is necessary are in the Technet article Configuring object cache user accounts. The article outlines the process for performing the configuration, but bizarrely (IMHO) lists a process which is half PowerShell, half manual steps in Central Admin. My guess is that, just like software, shipping documentation has compromises too, and this article didn’t yet make the cut to receive the full treatment. I’ve written some PowerShell which does the full configuration – 

Make sure you edit it to use your account names and web application URLs. You'll need to paste this into a tool which can strip out HTML, before saving as a .ps1 file. 

NOTE: In claims mode, you'll need to use the claims equivalent of the username otherwise you'll end up with SharePoint serving access denied errors to all people trying to access the site.

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

# EDIT THIS TO ENSURE ACCOUNTS ARE CORRECT FOR ENVIRONMENT..
$objectCachePortalSuperReader = "sp2010\portalsuperreader" 
$objectCachePortalSuperUser = "sp2010\portalsuperuser"  
# EDIT THIS TO ENSURE WEBAPPS ARE CORRECT FOR ENVIRONMENT..
$TescoWebApps = ("http://info.hub.tesco.org", "http://search.hub.tesco.org", "http://team.hub.tesco.org", "http://hub.tesco.org") Function ConfigureObjectCachingOnWebApp([Microsoft.SharePoint.Administration.SPWebApplication]$webApplication) { Write-Host ("Processing " + $webApplication.Url); try { GrantPolicy $webApplication $objectCachePortalSuperReader FullRead GrantPolicy $webApplication $objectCachePortalSuperUser FullControl SetPropertyOnWebApp $webApplication "portalsuperuseraccount" $objectCachePortalSuperUser SetPropertyOnWebApp $webApplication "portalsuperreaderaccount" $objectCachePortalSuperReader $webApplication.Update() Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Configured " + $webApplication.Url); } catch { Write-Host -BackgroundColor DarkRed -ForegroundColor White ("Failed to configure " + $webApplication.Url + ". Error details : " + $_) } } Function GrantPolicy([Microsoft.SharePoint.Administration.SPWebApplication]$webApplicationForPolicy, [string]$userOrGroup, [Microsoft.SharePoint.Administration.SPPolicyRoleType]$policyLevel) { $policy = $webApplicationForPolicy.Policies.Add($userOrGroup, $userOrGroup) $policy.PolicyRoleBindings.Add($webApplicationForPolicy.PolicyRoles.GetSpecialRole($policyLevel)) } Function SetPropertyOnWebApp([Microsoft.SharePoint.Administration.SPWebApplication]$webApplicationForProperty, [string]$property, [string]$propertyValue) { $webApplicationForProperty.Properties[$property] = $propertyValue } # inline script starts here.. $confirmed = Read-Host "This script must be edited before running for correct user accounts for each environment - have you done this? Enter Y to continue or N to exit." if ($confirmed -eq 'y') { foreach($webAppName in $TescoWebApps) { $webApp = Get-SPWebApplication -Identity $webAppName -ErrorAction SilentlyContinue if($webApp) { ConfigureObjectCachingOnWebApp($webApp) } else { Write-Host -ForegroundColor DarkRed ("Failed to find web app '" + $webAppName + "' in this environment. " + "(This is expected if it is a staging web app in the production environment, and vice versa.)") } } } Write-Host -BackgroundColor Blue -ForegroundColor White "Script completed"

No comments:

Post a Comment