locked
Delete Security role with Powershell from a Document Library RRS feed

  • Question

  • Hi All,

    I'm strungling with the following, which I though would be a simple enough task.

    I have a couple of hundred sites, on which I have a document library (inheritance broken) and I need to remove a group from each of this document libraries. This is on SharePoint Online, so I need to use the CSOM.

    After some stungles I came up with this:

    $Username = "UserName"
    $Site = "https://testurl/sites/TEST"
    $Password = Read-Host -Prompt "Please enter your password" -AsSecureString
    
    
    #Add references to SharePoint client assemblies and authenticate to Office 365 site
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
    $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
    $Context.Credentials = $Creds
    
    $Web = $Context.Web
    $Context.Load($web)
    
    $curlist=$context.Web.Lists.GetByTitle("Documenten")
    
    $Context.Load($curlist.RoleAssignments)
    
    $Context.ExecuteQuery()
    
    foreach ($ra in $curlist.RoleAssignments)
    {
        
        $p=$ra.member
        $Context.Load($p)
        $Context.ExecuteQuery()
    
        write-host $p.LoginName -ForegroundColor Cyan
        if ($p.LoginName -eq "Projects")
        {
            Write-Host "--> "$p.loginname  -ForegroundColor Green
            $ra.DeleteObject()
            $ra.Update()
           
            
        }
    
    }
    



    I does not generate any errors, and lists the security roles.. But I'm unable to delete the role 'Projects'... 

    Anybody a idea in which direction I need to search?

    thks

    Bart
    Monday, August 10, 2015 11:01 AM

Answers

All replies