-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetMailboxFolderPermissionReport.ps1
More file actions
271 lines (239 loc) · 10.9 KB
/
GetMailboxFolderPermissionReport.ps1
File metadata and controls
271 lines (239 loc) · 10.9 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<#
=============================================================================================
Name: Get Mailbox Folder Permission Report Using PowerShell
Version: 1.0
Website: o365reports.com
~~~~~~~~~~~~~~~~~
Script Highlights:
~~~~~~~~~~~~~~~~~
1. The script automatically installs the Exchange PowerShell module (if not installed already) upon your confirmation.
2. The script can generate 7+ folder permission reports.
3. Retrieves all mailbox folders and their permissions for all mailboxes.
4. Shows permission for a specific folder in all mailboxes.
5. Get a list of mailbox folders a user has access to.
6. Retrieves all mailbox folders delegated with specific access rights.
7. Provides option to exclude default and anonymous access.
8. Allows to get folder permissions for all user mailboxes.
9. Allows to get folder permissions for all shared mailboxes.
10. Exports report results to CSV.
11. The script is scheduler friendly.
12. It can be executed with certificate-based authentication (CBA) too.
For detailed script execution: https://o365reports.com/2024/06/05/get-mailbox-folder-permission-report-using-powershell/
============================================================================================
#>
Param (
[Parameter(Mandatory = $false)]
[string]$ClientId,
[string]$Organization,
[string]$CertificateThumbprint,
[string]$UserName,
[string]$Password,
[string]$MailboxUPN ,
[string]$MailboxCSV ,
[string]$SpecificFolder ,
[string]$FoldersUserCanAccess,
[ValidateSet("None","Reviewer","PublishingEditor","PublishingAuthor","Owner","NonEditingAuthor","Editor","Contributor","Author")]
[array]$AccessRights,
[switch]$ExcludeDefaultAndAnonymousUsers,
[switch]$UserMailboxOnly ,
[switch]$SharedMailboxOnly
)
#Check for EXO module inatallation
$Module = Get-Module ExchangeOnlineManagement -ListAvailable
if($Module.count -eq 0)
{
Write-Host Exchange Online PowerShell module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Write-host "Installing Exchange Online PowerShell module"
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force -Scope CurrentUser
}
else
{
Write-Host EXO module is required to connect Exchange Online.Please install module using Install-Module ExchangeOnlineManagement cmdlet.
Exit
}
}
Write-Host Connecting to Exchange Online...
if(($UserName -ne "") -and ($Password -ne ""))
{
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword
Connect-ExchangeOnline -Credential $Credential -ShowBanner:$false
}
elseif($Organization -ne "" -and $ClientId -ne "" -and $CertificateThumbprint -ne "")
{
Connect-ExchangeOnline -AppId $ClientId -CertificateThumbprint $CertificateThumbprint -Organization $Organization -ShowBanner:$false
}
else
{
Connect-ExchangeOnline -ShowBanner:$false
}
#Output file declaration
$Location = (Get-Location)
$OutputCSV = "$($Location)\MailboxFolderPermissionReport_$((Get-Date -format 'yyyy-MMM-dd-ddd hh-mm-ss').ToString()).csv"
#Function to export csv in mailbox folder permissions
Function GetPermission {
param($MailboxUPN, $FolderPermissions)
foreach ($Permission in $FolderPermissions) {
$MailboxFolderPermissionsData = [PSCustomObject]@{
"Display Name" =$DisplayName
"UPN" = $MailboxUPN
"Mailbox Type" = $MailboxType
"Folder Name" = $Permission.FolderName
"Folder Identity" = $Permission.Identity
"Shared To" = $Permission.User
"Access Rights" =$Permission.AccessRights.Trim('{', '}')
}
$MailboxFolderPermissionsData | Export-Csv -Path "$OutputCSV" -Append -NoTypeInformation -Force
}
}
#Function to get mailbox folder names
Function FolderStatistics{
param($MailboxUPN)
Get-EXOMailboxFolderStatistics -Identity $MailboxUPN | Foreach {
$FolderIdentity =$_.Identity
$FolderName =$FolderIdentity.Substring($FolderIdentity.IndexOf('\')+1)
if($FolderName -like "Top of Information Store"){
$FolderName =""
}
elseif($FolderName -in "Recoverable Items","Audits","Calendar Logging","Deletions","Purges","Versions","SubstrateHolds","DiscoveryHolds"){
return
}
if($ExcludeDefaultAndAnonymousUsers.IsPresent){
ProcessExcludeDefaultAndAnonymousUsers -MailboxUPN $MailboxUPN -Foldername $FolderName
}
elseif($FoldersUserCanAccess -ne ""){
ProcessFoldersUserCanAccess -MailboxUPN $MailboxUPN -Foldername $FolderName -User $FoldersUserCanAccess
}
elseif($AccessRights.Count -gt 0) {
ProcessToFilterFolderPermissionsByAccessRights -MailboxUPN $MailboxUPN -AccessRights $AccessRights -Foldername $FolderName
}
else{
ProcessAllMailboxFolderPermission -MailboxUPN $MailboxUPN -Foldername $FolderName
}
}
}
#Function to permission for all mailbox folders
Function ProcessAllMailboxFolderPermission {
param($MailboxUPN,$Foldername)
$FolderPermissions = Get-EXOMailboxFolderPermission -Identity "${MailboxUPN}:\${FolderName}"
GetPermission -MailboxUPN $MailboxUPN -FolderPermissions $FolderPermissions
}
#Function to mailbox permissions for particular folders
Function ProcessSpecificMailboxFolderPermission {
param($MailboxUPN, $FolderName)
$FolderPermissions = Get-EXOMailboxFolderPermission -Identity "${MailboxUPN}:\${FolderName}"
if($FolderPermissions){
GetPermission -MailboxUPN $MailboxUPN -FolderPermissions $FolderPermissions
}
else{
Write-Host "Failed to Specific folder statistics for mailbox: $MailboxUPN IN $SpecificFolder" -ForegroundColor Yellow
}
}
# Function to mailbox folders user permission
Function ProcessExcludeDefaultAndAnonymousUsers{
param($MailboxUPN,$FolderName )
$FolderPermissions = Get-EXOMailboxFolderPermission -Identity "${MailboxUPN}:\${FolderName}"| Where-Object { $_.User -notin @("Default", "Anonymous") }
GetPermission -MailboxUPN $MailboxUPN -FolderPermissions $FolderPermissions
}
#Function to Identify the particular user in the mailbox folders permission
Function ProcessFoldersUserCanAccess{
param($MailboxUPN,$FolderName ,$User)
$FolderPermissions = Get-EXOMailboxFolderPermission -Identity "${MailboxUPN}:\${FolderName}" -User $User -ErrorAction SilentlyContinue
GetPermission -MailboxUPN $MailboxUPN -FolderPermissions $FolderPermissions
}
#Function to retrieve permissions for all folders with a specific access right
Function ProcessToFilterFolderPermissionsByAccessRights {
param(
$MailboxUPN,
$AccessRights,
$FolderName
)
$FolderPermissions = Get-MailboxFolderPermission -Identity "${MailboxUPN}:\${FolderName}"| Where-Object { $_.AccessRights -in $AccessRights}
if($FolderPermissions){
GetPermission -mailboxUPN $MailboxUPN -FolderPermissions $FolderPermissions
}
}
#Function to get Mailbox
Function Getmailbox{
Param($MailboxUPN)
$MailBoxInfo = Get-EXOMailbox -UserPrincipalName $MailboxUPN
$DisplayName =$MailboxInfo.DisplayName
$MailboxType = $MailboxInfo.RecipientTypeDetails
Process-Mailbox -MailboxUPN $MailboxUPN
}
Function Process-Mailbox{
Param ($MailboxUPN)
Write-Progress -Activity "Processed Mailbox Count : $ProgressIndex" -Status "Currently Processing : $MailboxUPN"
if($SpecificFolder -ne ""){
ProcessSpecificMailboxFolderPermission -MailboxUPN $MailboxUPN -FolderName $SpecificFolder
}
else{
FolderStatistics -MailboxUPN $MailboxUPN
}
}
#Single mailboxUPN
if ($MailboxUPN) {
$ProgressIndex =1
Getmailbox -MailboxUPN $MailboxUPN
}
#CSV file input
elseif($MailBoxCSV) {
$Mailboxes = Import-Csv -Path $MailBoxCSV
$ProgressIndex = 0
foreach ($Mailbox in $Mailboxes) {
$ProgressIndex++
Getmailbox -MailboxUPN $Mailbox.Mailboxes
}
}
else{
$ProgressIndex =0
if($SharedMailboxOnly.IsPresent -or $UserMailboxOnly.IsPresent){
if ($SharedMailboxOnly.IsPresent) {
$RecipientType ="SharedMailbox"
}
else {
$RecipientType = "UserMailbox"
}
Get-EXOMailbox -RecipientTypeDetails $RecipientType -ResultSize Unlimited| Foreach {
$ProgressIndex++
$DisplayName =$_.DisplayName
$MailboxUPN = $_.UserPrincipalName
$MailboxType = $_.RecipientTypeDetails
Process-Mailbox -MailboxUPN $MailboxUPN
}
}
else{
Get-EXOMailbox -ResultSize Unlimited| Foreach {
$ProgressIndex++
$DisplayName =$_.DisplayName
$MailboxUPN = $_.UserPrincipalName
$MailboxType = $_.RecipientTypeDetails
Process-Mailbox -MailboxUPN $MailboxUPN
}
}
}
if (Test-Path -Path $OutputCSV) {
$ItemCounts = (Import-Csv -Path "$OutputCSV").Count
if($ItemCounts -ne 0){
Write-Host "`nThe output file contains $($ProgressIndex) mailboxes and $($ItemCounts) records." -ForegroundColor Cyan
Write-Host "`nThe output file is available at:" -ForegroundColor Yellow ;Write-Host $OutputCSV -ForegroundColor Cyan
Write-Host "`n~~ Script prepared by AdminDroid Community ~~`n" -ForegroundColor Green
Write-Host "~~ Check out " -NoNewline -ForegroundColor Green
Write-Host "admindroid.com" -ForegroundColor Yellow -NoNewline
Write-Host " to get access to 1800+ Microsoft 365 reports. ~~" -ForegroundColor Green
Write-Host "`n`n"
$Prompt = New-Object -ComObject wscript.shell
$UserInput = $Prompt.popup("Do you want to open the output file?", 0, "Open Output File", 4)
if ($UserInput -eq 6) {
Invoke-Item "$OutputCSV"
}
}
}
else {
Write-Host "No records found" -ForegroundColor Yellow
}
#Disconnect Exchange Online session
Disconnect-ExchangeOnline -Confirm:$false