0

I'm trying to get the below powershell script to work but getting this error :

The term 'Get-PnPRoleAssignment' is not recognized as a name of a cmdlet It seems this command does not exist and if not whats the alternative

Snippet code below

# Required modules
#Import-Module PnP.PowerShell
Import-Module ImportExcel

# Output file path
$outputFile = "C:\Remote\Export\SharePointPermissionsMatrix.xlsx"
$results = @()

# Connect to your root SharePoint admin site
Connect-PnPOnline -Url "https://xxx-admin.sharepoint.com" -Interactive -ClientId xxxxxxx

# Get all site collections
$sites = Get-PnPTenantSite | Where-Object { $_.Url -like "https://xxx.sharepoint.com/sites/*" }

foreach ($site in $sites) {
    Write-Host "`nProcessing Site: $($site.Url)" -ForegroundColor Cyan

    Connect-PnPOnline -Url $site.Url -Interactive -ClientId 
XXXXXX

    $lists = Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 }

    foreach ($list in $lists) {
        Write-Host "  Checking library: $($list.Title)" -ForegroundColor Yellow

        if ($list.HasUniqueRoleAssignments) {
            $roleAssignments = Get-PnPRoleAssignment -List $list

            foreach ($roleAssignment in $roleAssignments) {
                $results += [PSCustomObject]@{
                    SiteUrl        = $site.Url
                    LibraryTitle   = $list.Title
                    Principal      = $roleAssignment.Principal.Title
                    Role           = ($roleAssignment.RoleDefinitionBindings | ForEach-Object { $_.Name }) -join ", "
                    PermissionType = "Unique"
                }
            }
        } else {
            # Inherited from site level
            $siteRoleAssignments = Get-PnPRoleAssignment

            foreach ($siteRole in $siteRoleAssignments) {
                $results += [PSCustomObject]@{
                    SiteUrl        = $site.Url
                    LibraryTitle   = $list.Title
                    Principal      = $siteRole.Principal.Title
                    Role           = ($siteRole.RoleDefinitionBindings | ForEach-Object { $_.Name }) -join ", "
                    PermissionType = "Inherited"
                }
            }
        }
    }
}

# Export to Excel
$results | Export-Excel -Path $outputFile 

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.