Mon. Jan 3rd, 2022
Function SetSiteToReadyOnly([String]$SiteURL,$Credentials,$reportYN)
{
    Try
    {
        $PermissionToAdd = "Read Policy"
        $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)
        $AddPermissionRole = $web.RoleDefinitions.GetByName($PermissionToAdd)
        $ctx.Load($web.RoleAssignments)
        $ctx.ExecuteQuery();

        #Get all Groups
        $Groups=$ctx.Web.SiteGroups
        $ctx.Load($Groups)
        $ctx.ExecuteQuery()
        foreach ($roleAssignment in $web.RoleAssignments)
        {
            SetRoleAssignmentToReadOnly -web $web -ctx $ctx -roleAssignment $roleAssignment -AddPermissionRole $AddPermissionRole
        }

        $ctx.Load($web.Lists)
        $ctx.ExecuteQuery();
 
        foreach ($list in $web.Lists)
        {
            write-host $List.Title - "Checking List permissions" -ForegroundColor Yellow
            if ($Global:listExclusions.Contains($List.Title) -eq $false)
            {
                Invoke-LoadMethod -Object $list -PropertyName "HasUniqueRoleAssignments"
                $ctx.ExecuteQuery()

                if ($list.HasUniqueRoleAssignments -and $list.Hidden -eq $false)
                {
                    write-host "$($list.Title) has unique permissions" -ForegroundColor Yellow
                    $ctx.Load($list.RoleAssignments)
                    $ctx.ExecuteQuery();

                    foreach ($roleAssignment in $list.RoleAssignments)
                    {
                        SetRoleAssignmentToReadOnly -web $web -ctx $ctx -roleAssignment $roleAssignment -AddPermissionRole $AddPermissionRole
                    }

                }
$AddPermissionRole #-itemPermissionsSheet $itemPermissionsSheet
            }
        }
    }
    Catch{
        write-host -f Red "Error !" $SiteURL $_.Exception.Message
    }
}

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