Tue. Jan 4th, 2022
Function BreakSiteInheritance([String]$SiteURL,$Credentials,$reportYN)
{
    Try
    {
        $WebsCount=0
        $Timestamp = GetTimeStamp
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        Write-host "Connected to $SiteURL" -ForegroundColor Green
        
        $ctx.Credentials = $credentials;

        $Web = $ctx.Web
        $ctx.Load($web)
        $ctx.Load($web.Webs)
        $ctx.Load($web.RoleAssignments)
        Invoke-LoadMethod -Object $web -PropertyName "HasUniqueRoleAssignments"
        $ctx.ExecuteQuery()

        if ($web.HasUniqueRoleAssignments)
        {
            write-host -f yellow "$($web.title) has unique permissions"
        }else
        {
            write-host -f red "$($web.title) does not have unique permissions"
            #Break Permission inheritance of the Web - use existing groups from parent
            $Web.BreakRoleInheritance($True, $False)
            $Ctx.executeQuery()
            write-host -f yellow "Permission Broken for site $($SiteURL)"
        }

        #Iterate through each subsite in the current web
        foreach ($Subweb in $web.Webs)
        {
                $WebsCount++
                #Call the function recursively to process all subsites underneaththe current web
                Break-InheritanceSubWebs($SubWeb.URL)
        }
    }
    Catch{
        write-host -f Red "Error ! unable to break permissions for site - $($SiteURL)" $_.Exception.Message
    }
}

This website uses cookies. By continuing to use this site, you accept our use of cookies.