Last active
November 15, 2020 01:33
-
-
Save zplume/daae063035b568f62440154ec75a311b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory = $true)] | |
[string]$ListTitle, | |
[Parameter(Mandatory = $true)] | |
[int]$ItemId, | |
[Parameter(Mandatory = $true)] | |
[string]$Path | |
) | |
# You'll need to use Connect-PnPOnline before running the script :) | |
# Stop on first (unhandled) Exception | |
$ErrorActionPreference = "Stop" | |
$item = Get-PnPListItem -List $ListTitle -Id $ItemId | |
# Load role assignments | |
$roleAssignments = Get-PnPProperty -ClientObject $item -Property "RoleAssignments" | |
# Array to store output | |
$rows = @() | |
# Loop over role assignments, load bindings | |
foreach($ra in $roleAssignments) { | |
$userOrGroupId = $ra.PrincipalId | |
# Retrieve the user or group item from the User Information List by its item ID | |
$userOrGroupItem = Get-PnPListItem -List "User Information List" -Id $ra.PrincipalId -Fields "Title", "Name", "EMail" | |
# Load role definition bindings | |
$bindings = Get-PnPProperty -ClientObject $ra -Property "RoleDefinitionBindings" | |
# Get role definition binding names as a string array | |
$bindingNames = $bindings | Select-Object -Property "Name" -ExpandProperty "Name" | |
# Create HashTable of useful properties | |
$row = @{ | |
"Principal Id" = $ra.PrincipalId; | |
"Principal Name" = $userOrGroupItem.FieldValues["Title"]; | |
"Principal Login" = $userOrGroupItem.FieldValues["Name"]; | |
"Principal Email" = $userOrGroupItem.FieldValues["EMail"]; | |
"RoleDefinitionBindings" = [string]::Join(", ", $bindingNames); | |
} | |
# Convert to PSObject and add to $rows with columns in the order specified | |
$rows += New-Object psobject -Property $row | Select-Object -Property "Principal Id", "Principal Name", "Principal Email", "RoleDefinitionBindings" | |
} | |
# Save data as CSV | |
$rows | Export-Csv -NoTypeInformation -Path $Path -Force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment