0

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.

2
  • Do you have PowerShell setup? Commented Jan 27, 2015 at 15:40
  • yes, but I think I need to get a newer version as we have PowerShell 1.0 installed. Commented Jan 28, 2015 at 12:49

1 Answer 1

0

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
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.