TransWikia.com

Powershell CSOM to retrieve Permission levels tagged for the group

SharePoint Asked by Suresh Bolineni on December 12, 2021

Can any one code me to retrieve the permission levels tagged for the group using Powershell CSOM.Please tell me the property
her is my code sample

Add-Type -Path "C:\Microsoft.SharePoint.Client.dll"   
Add-Type -Path "C:\Microsoft.SharePoint.Client.Runtime.dll"      

    #SPO Client Object Model Context 
    $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) 
    $spoCredentials = New-Object System.Net.NetworkCredential($sUserName, $sPassword)   
    $spoCtx.Credentials = $spoCredentials       

    $web = $spoCtx.Web
    $spGroups =$web.SiteGroups
    $spoCtx.Load($spGroups)
    $spoCtx.ExecuteQuery() 

    foreach($group in $spGroups)
    {
        #Write-Host " Group Name = $($group.Title)"

    }

One Answer

I have found the solution to retrieve the Permission levels using RoleAssignments and RoleDefinition here is my code

Function Get-Permissions([Microsoft.SharePoint.Client.ClientContext]$Object ,[Microsoft.SharePoint.Client.RoleAssignmentCollection]$RoleAssignmentsCol)
{       
        $spGroups=@()
        $spoCtx = $Object

        foreach($RoleAssignment in $RoleAssignmentsCol)
        {
            $RoleDefinitionBindings = $RoleAssignment.RoleDefinitionBindings
            $RoleAssignmentMember = $RoleAssignment.Member

            $spoCtx.Load($RoleDefinitionBindings)
            $spoCtx.Load($RoleAssignmentMember)
            $spoCtx.ExecuteQuery()


            foreach ($RoleDefinition in  $RoleDefinitionBindings)
            {
                $GroupProperty = New-Object System.Object
                $GroupProperty | Add-Member -type NoteProperty -name LoginName -value $RoleAssignmentMember.LoginName
                $GroupProperty | Add-Member -type NoteProperty -name PrincipalType -value ([Microsoft.SharePoint.Client.Principal]$RoleAssignmentMember).PrincipalType
                $GroupProperty | Add-Member -type NoteProperty -name PermissionLevel -value  $RoleDefinition.Name
                $spGroups += $GroupProperty
            }
        }

        return $spGroups
}


function GetSitePermisions
{
 try 
    {
        Add-Type -Path "D:Microsoft.SharePoint.Client.dll"   
        Add-Type -Path "D:Microsoft.SharePoint.Client.Runtime.dll"      

        $sSiteUrl = Read-Host -Prompt "Enter Site Collection URL (eg: http://Server:port/Sites/Dev)" 
        $sUserName = Read-Host -Prompt "Enter User Name(eg: domainusername)" 
        $sPassword = Read-Host -Prompt "Enter your password" -AsSecureString 

        #SPO Client Object Model Context 
        $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) 
        $spoCredentials = New-Object System.Net.NetworkCredential($sUserName, $sPassword)   
        $spoCtx.Credentials = $spoCredentials       

        $web = $spoCtx.Web
        $RoleAssignmentsCol = $web.RoleAssignments      
        $spoCtx.Load($web)

        $spoCtx.Load($RoleAssignmentsCol)
        $spoCtx.ExecuteQuery() 

        $GrouppermissionLevel = @()

        Write-Host "getting Permission Level details"
        $spGroups = Get-Permissions $spoCtx $RoleAssignmentsCol

        foreach($group in $spGroups)
        {
            $GrouppermissionLevel = New-Object -TypeName PSObject -Property @{
                                    GroupName = $group.LoginName
                                    PrincipalType= $group.PrincipalType
                                    PermissionLevel = $group.PermissionLevel
                                    } | Select  GroupName,PrincipalType,PermissionLevel

            $GrouppermissionLevel| Export-CSV ("D:GroupPermissionLevel.csv") -NoTypeInformation -Append  
        }
    } 
    catch [System.Exception] 
    { 
        Write-Host -ForegroundColor Red $_.Exception.ToString()    
    }  
}

GetSitePermisions ;

Answered by Suresh Bolineni on December 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP