Skip to content

Instantly share code, notes, and snippets.

@zplume
Last active November 15, 2020 01:33
Show Gist options
  • Save zplume/daae063035b568f62440154ec75a311b to your computer and use it in GitHub Desktop.
Save zplume/daae063035b568f62440154ec75a311b to your computer and use it in GitHub Desktop.
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