Udostępnij za pośrednictwem


PowerShell for adding zone user policy for a web application

Sharing a PowerShell script that I wrote for adding zone user policy for a web application.

 Function New-SPUserPolicy
{
    Param([string] $Name,
          [string] $Zone,
          [string] $UserName,
          [string] $DisplayName,
          [string[]] $Permissions
         )         

    Write-Host -ForeGroundColor White "- Getting Web Application"
    $WebApp = Get-SPWebApplication  $Name 
    $UrlZone = [Enum]::Parse([Microsoft.SharePoint.Administration.SPUrlZone], $Zone)
    Write-Host -ForeGroundColor White "- Getting Zone ($Zone) Policies"
    $Policies = $WebApp.ZonePolicies($UrlZone)
    Write-Host -ForeGroundColor White "- Creating a new Zone User Policy"
    [Microsoft.SharePoint.Administration.SPPolicy] $Policy = $Policies.Add($UserName, $DisplayName)
    
    ForEach($Perm in $Permissions)
    {
        [Microsoft.SharePoint.Administration.SPPolicyRole] $Role = $WebApp.PolicyRoles[$Perm]
        If($Role -eq $null) { Throw "Invalid Permission - $Perm" }
        Write-Host -ForeGroundColor White "- Adding $Perm to Policy Role Bindings"
        $Policy.PolicyRoleBindings.Add($Role)
    }
    Write-Host -ForeGroundColor White "- Updating Web Application"
    $WebApp.Update()
}

 

Cheers,

</Ram>