Function Break-InheritanceSubWebs($WebURL)
{
Try{
#Set up the context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
Write-host "Connected to $WebURL" -ForegroundColor Green
$ctx.Credentials = $Credentials
$Web = $ctx.Web
$ctx.Load($web)
$ctx.Load($web.Webs)
$ctx.ExecuteQuery()
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)
{
#Call the function recursively to process all subsites underneaththe current web
Break-InheritanceSubWebs($SubWeb.URL)
}
}catch
{
write-host -f Red "199 Error ! unable to break permissions for site - $($WebURL)" $_.Exception.Message
}
}