-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharePointExportPermissions.ps1
130 lines (120 loc) · 5.14 KB
/
SharePointExportPermissions.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string[]]
$URLs,
[Parameter(Mandatory = $false)]
[PSCredential]
$Credential = (Get-Credential)
)
BEGIN {
$ErrorActionPreference = "stop"
Connect-AzureAd -Credential $Credential | Out-Null
function ConnectPNPOnline ($URL, [PSCredential]$Credential) {
Try {
Disconnect-PnPOnline
}
Catch {}
try {
Connect-PnPOnline -Url $URL -Credentials $Credential
}
Catch {
$err = $_
Write-Host "Failed to connect to URL $URL. Error: $err" -ForegroundColor Red
Throw "Error"
}
}
function GetAzureAdGroupMember ($LoginName) {
$ObjectId = ($LoginName -split "\|")[-1]
try {
$GroupMembers = Get-AzureAdGroupMember -ObjectId $ObjectId
$GroupMembers = $GroupMembers.mail
}
Catch {
$GroupMembers = @()
}
return $GroupMembers
}
function GetPnPDocumentLibraries() {
$LibrariesToSkip = @("Documents", "Form Templates", "Site Assets", "Site Pages", "Style Library")
$Libraries = Get-PnPList | Where-Object { $_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false } | Where-Object { $_.Title -notin $LibrariesToSkip }
return $Libraries
}
function GetRoleAssignments($Library) {
$RoleAssignments = $Library.RoleAssignments
$PermissionCollection = @()
Foreach ($RoleAssignment in $RoleAssignments) {
Get-PnPProperty -ClientObject $RoleAssignment -Property RoleDefinitionBindings, Member
$PrincipalType = $RoleAssignment.Member.PrincipalType
$PermissionLevels = $RoleAssignment.RoleDefinitionBindings | Select-Object -ExpandProperty Name
If ($PrincipalType -eq "SharePointGroup") {
$GroupMembers = Get-PnPGroupMember -Identity $RoleAssignment.Member.LoginName | Where-Object Email | Select-Object -ExpandProperty Email
if (($GroupMembers | Measure-Object | Select-Object -ExpandProperty Count) -eq 0) { continue }
$PermissionCollection += [PSCustomObject]@{
URL = $Library.RootFolder.ServerRelativeUrl
LibraryName = $Library.Title
PrincipalName = $RoleAssignment.Member.Title
PrincipalType = $PrincipalType
Permissions = $PermissionLevels -join " | "
Membership = $GroupMembers -join " | "
}
}
elseif ($PrincipalType -eq "SecurityGroup") {
$GroupMembers = GetAzureAdGroupMember -LoginName $RoleAssignment.Member.LoginName
if (($GroupMembers | Measure-Object | Select-Object -ExpandProperty Count) -eq 0) { continue }
$PermissionCollection += [PSCustomObject]@{
URL = $Library.RootFolder.ServerRelativeUrl
LibraryName = $Library.Title
PrincipalName = $RoleAssignment.Member.Title
PrincipalType = $PrincipalType
Permissions = $PermissionLevels -join " | "
Membership = $GroupMembers -join " | "
}
}
Else {
$PermissionCollection += [PSCustomObject]@{
URL = $Library.RootFolder.ServerRelativeUrl
LibraryName = $Library.Title
PrincipalName = $RoleAssignment.Member.Title
PrincipalType = $PrincipalType
Permissions = $PermissionLevels -join " | "
Membership = "N/A"
}
}
}
return $PermissionCollection
}
}
PROCESS {
$Total = $URLs | Measure-Object | Select-Object -ExpandProperty Count
$i = 0
$Results = @()
foreach ($URL in $URLs) {
Write-Progress -Id 1 -Activity "Exporting SharePoint Permissions..." -Status "Sites: [$i / $Total] | Site: $URL" -PercentComplete (($i / $Total) * 100)
$i++
try {
ConnectPNPOnline -URL $URL -Credential $Credential
}
Catch {
Continue
}
$Libraries = GetPnPDocumentLibraries
$SubTotal = $Libraries | Measure-Object | Select-Object -ExpandProperty Count
$j = 0
foreach ($Library in $Libraries) {
Write-Progress -Id 2 -ParentId 1 -Activity "Exporting Document Library Permissions..." -Status "Libraries: [$j / $SubTotal] | Library: $($Library.Title)" -PercentComplete (($j / $SubTotal) * 100)
$j++
$Library = Get-PnPList -Identity $Library.Title -Includes RoleAssignments
$Results += GetRoleAssignments -Library $Library
}
Write-Progress -Id 2 -Completed -Activity "Exporting Document Library Permissions..."
}
}
END {
if ($Results) {
$Date = Get-Date -Format yyyy-MM-dd_HH.mm
$FileName = "SharePoint_Permissions_Export_$Date.csv"
$Results | Export-Csv -NoTypeInformation -LiteralPath $FileName
Write-Host "File exported to: $FileName" -ForegroundColor Green
}
}