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
}
}


PowerShell: Activate feature on all site levels.


#
# Description:
# Activate the site feature on all sub-sites
# This feature creates custom list
#
# Created Date:
# v1.0 - 27-11-2017
#
# Author:
# Vikas Bansal
#
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#feature ID to create custom list
$FeatureID="3e0d7f50-a6b7-45a6-b5a1-77950ef4327f"
#get all webs (sub-sites) under site collection
$WebsColl = Get-SPSite https://sp2013.com | Get-SPWeb -Limit ALL
#get the Feature
$Feature = Get-SPFeature -Limit All | Where { $_.ID -eq $FeatureID }
#check if Feature exists
if($Feature -ne $null) {
Write-Host "feature exists" -ForegroundColor DarkBlue -BackgroundColor Yellow
#iterate through through each sub-site activate the feature
foreach($web in $WebsColl){
#check if the Feature is activated
if (Get-SPFeature -web $web.Url | Where {$_.ID -eq $FeatureId}) {
Write-Host "feature is already activated on " $web.Url -ForegroundColor DarkBlue -BackgroundColor Yellow
}
else {
Write-Host "feature not activated on " $web.Url -ForegroundColor Yellow -BackgroundColor Red
#Activate the Feature
Enable-SPFeature –identity $FeatureID -URL $web.URL -Confirm:$false
Write-Host "feature is activated now on " $web.Url -ForegroundColor DarkBlue -BackgroundColor White
}
}
}
else {
Write-Host "feature does not exists" -ForegroundColor Yellow -BackgroundColor Red
}