On a SharePoint 2007 portal with hundreds of site collections with nested subsites, I need to set permissions to inherit from top level sites.
Is there a way to do this with a script? Going through each site manually would take ages.
On a SharePoint 2007 portal with hundreds of site collections with nested subsites, I need to set permissions to inherit from top level sites.
Is there a way to do this with a script? Going through each site manually would take ages.
I found below script which can do the task for you
### this script will make a specified site and all its child objects (sites, lists, folders & items) inherit permissions from that site ###
# WARNING: Please specify the correct top-level site or subsite - All permissions will be deleted from that specified URL downwards
# enter the new permissions in the following format $newpermissions["DOMAIN\username"] = "Security Level"
# example: $newpermissions["mydomain\user"] = "Read" will give read permissions to mydomain\user on specified URL -AND- all its child objects.
##### PARAMETERS #####
$siteurl = "http://my.sharepoint.site"
$newpermissions = @{}
$newpermissions["mydomain\user"] = "Contribute"
$newpermissions["SHAREPOINT\system"] = "Full Control"
$newpermissions["NT Authority\Authenticated Users"] = "Read"
##### SCRIPT #####
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
function CheckWebAndSiblings($web)
{
try
{
write-host "Checking web $($web.Title).. " -nonewline -foreground blue
if($web.HasUniqueRoleAssignments)
{
$web.ResetRoleInheritance()
write-host "updated" -foreground green
}
else
{
write-host "no update required" -foreground red
}
foreach($list in $web.Lists | where-object { $_.Hidden -eq $false -and $_.AllowDeletion -eq $true })
{
write-host "> Checking list $($list.Title).. " -nonewline -foreground darkcyan
if($list.HasUniqueRoleAssignments)
{
$list.ResetRoleInheritance()
write-host "updated" -foreground green
}
else
{
write-host "no update required" -foreground red
}
foreach($folder in $list.Folders)
{
write-host "> > Checking folder $($folder.Name).. " -nonewline
if($folder.HasUniqueRoleAssignments)
{
$folder.ResetRoleInheritance()
write-host "updated" -foreground green
}
else
{
write-host "no update required" -foreground red
}
}
foreach($item in $list.Items)
{
write-host "> > Checking item $($item.Name).. " -nonewline
if($item.HasUniqueRoleAssignments)
{
$item.ResetRoleInheritance()
write-host "updated" -foreground green
}
else
{
write-host "no update required" -foreground red
}
}
}
}
catch
{
if($error[0].Exception.Message -like "*Cannot change permissions of root web*")
{
write-host "Skipping rootweb as permissions needs to be set here" -foreground red
}
else
{
write-host $error[0].Exception.Message -foreground red
}
}
finally
{
foreach($subweb in $web.Webs)
{
CheckWebAndSiblings($subweb)
}
}
}
$site = new-object Microsoft.SharePoint.SPSite($siteurl)
$rootweb = $site.OpenWeb()
CheckWebAndSiblings($rootweb)
write-host "- - - - - - - Applying new permissions - - - - - - -" -foreground blue
for($i = 0; $i -lt $rootweb.RoleAssignments.Count; $i++)
{
$web.RoleAssignments.Remove(0)
}
foreach($hash in $newpermissions.GetEnumerator())
{
write-host "$($hash.Key) -> $($hash.Value).. " -nonewline
$ra = new-object Microsoft.SharePoint.SPRoleAssignment($hash.Key, "", "", "")
$rd = $rootweb.Site.RootWeb.RoleDefinitions[$hash.Value]
$ra.RoleDefinitionBindings.Add($rd)
$rootweb.RoleAssignments.Add($ra)
write-host "done" -foreground green
}
$rootweb.Update()
$rootweb.Dispose()
$site.Dispose()
write-host "-- completed --" -foreground green