Add User Policy using Powershell

 

 

# This script will add to user policy for a web application in SharePoint 2010

# and will add few more things to get rid of this event in the event logs:

# Object Cache: The super reader account utilized by the cache does not have sufficient permissions to SharePoint databases.

#  See, https://technet.microsoft.com/en-us/library/ff758656.aspx

 

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

function SetPolicy ($userOrGroup, $wa)

{

$user = New-SPClaimsPrincipal -IdentityType WindowsSamAccountName -Identity $userOrGroup

$policy = $wa.Policies.Add($user.ToEncodedString(), $userOrGroup)  

$policy.PolicyRoleBindings.Add($wa.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl))  

}

$superUserAccount = "Domain\SuperUserAccount"

$superReaderAccount = "Domain\ServiceMOSSInternet"

$wa = Get-SPWebApplication -Identity "https://www.contoso.com"

SetPolicy -userOrGroup $(whoami) -wa $wa

SetPolicy -userOrGroup $superUserAccount -wa $wa

SetPolicy -userOrGroup $superReaderAccount -wa $wa

$wa.Properties["portalsuperuseraccount"] = $superUserAccount

$wa.Properties["portalsuperreaderaccount"] = $superReaderAccount

$wa.Update()