I needed to add a group on all subwebsites within a sitecollection but on one library within those subwebs this group should have no access.
This is how my structure looks like:
SiteCollection
- Web1 (Special Permission)
-- Library 1 (Inherits from Web1)
-- Library 2 (Inherits from Web1)
- Web2 (Special Permission)
-- Library 1 (Inherits from Web2)
-- Library 2 (Inherits from Web1)
And this is how it should look like after the execution of the script:
SiteCollection
- Web1 (Special Permission)
-- Library 1 (Inherits from Web1)
-- Library 2 (Special Permissions)
- Web2 (Special Permission)
-- Library 1 (Inherits from Web2)
-- Library 2 (Special Permissions)
This PowerShell Script solves this task:
<#
.SYNOPSIS
add a group on all subwebsites within a sitecollection but on one library within those subwebs this group should have no access
.DESCRIPTION
add a group on all subwebsites within a sitecollection but on one library within those subwebs this group should have no access
.NOTES
Author : Daniel Sirz
.LINK
http://www.enterprisebugs.com
#>
$siteCollection = Get-SPSite
# Loop over all subwebs
foreach ($spWeb in $siteCollection.AllWebs)
{
if (!$spWeb.IsRootWeb)
{
# Assumption: The group is already present in sitecollection
$groupName = "Special Group"
$account = $spWeb.SiteGroups[$groupName]
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$role = $spWeb.RoleDefinitions["Mitwirken"]
$assignment.RoleDefinitionBindings.Add($role);
# Assign the role to the complete web
$spWeb.RoleAssignments.Add($assignment)
# Get the List
$Library2 = $spWeb.Lists["Library2"]
# Break Role Inheritance to have unique permissions for this list
$Library2.BreakRoleInheritance($true)
# Remove the role from this special list
$Library2.RoleAssignments.Remove($account)
$Library2.Update()
}
$spWeb.Dispose()
}
$siteCollection.Dispose()