Monday, 25 December 2017

PowerShell: Assign permission to read-only users on the list on all site levels.


#
# Description:
# List-Level Permission settings and assign "Contribute" role definition to the visitors group.
# To allow read-only users to post, update, delete comments.
#
# Created Date:
# v1.0 - 07-12-2017
#
# Author:
# Vikas Bansal
#
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$listName = "TestList"
#get all webs (sub-sites) under site collection
$WebsColl = Get-SPSite https://sp2013.com | Get-SPWeb -Limit ALL
#iterate through each sub-site change list permissions
foreach($web in $WebsColl) {
$commentsList = $web.Lists.TryGetList($listName)
if($commentsList -ne $null)
{
Write-Host $listName " exists on the" $web.Title at URL $web.Url -ForegroundColor DarkBlue -BackgroundColor Yellow
# Assign the "Contribute" RoleDefinition to the site's visitors group
$visitorsSPGroup = $commentsList.ParentWeb.AssociatedVisitorGroup #$spWeb.Groups[$visitorsSPGroupName]
if(!$commentsList.HasUniqueRoleAssignments) {
$commentsList.BreakRoleInheritance($true)
}
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($visitorsSPGroup)
$assignment.RoleDefinitionBindings.Add(($commentsList.ParentWeb.RoleDefinitions | Where-Object { $_.Type -eq "Contributor" }))
$commentsList.RoleAssignments.Add($assignment)
$commentsList.Update()
Write-Host $listName " permission is changed for readonly users on the" $web.Title at URL $web.Url -ForegroundColor DarkGreen -BackgroundColor White
}
else
{
Write-Host $listName " not exists on the" $web.Title at URL $web.Url -ForegroundColor Yellow -BackgroundColor Red
}
}


No comments:

Post a Comment