-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-MailboxPermissionMatrix.ps1
176 lines (143 loc) · 5.23 KB
/
Get-MailboxPermissionMatrix.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
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
<#
.SYNOPSIS
Gives an overview of all mailbox permission using a matrix table.
.DESCRIPTION
Attention: You need to login to an Exchange Online Session first, using the PowerShell Exchange Online module.
Returns an array of pscustomobjects displaying a matrix of all mailbox permissions by default.
Outputs the same as a CSV file if a path has been specified using the OutputFile parameter.
.PARAMETER OutputFile
Specifies the path to the output file. This includes the name of the file, as well as the file extension CSV.
.EXAMPLE
PS C:\> .\Get-MailboxPermissionMatrix.ps1
Returns an array of pscustomobjects of all mailbox permissions.
.EXAMPLE
PS C:\> .\Get-MailboxPermissionMatrix.ps1 | Format-Table
Returns an array of pscustomobjects of all mailbox permissions and formats it in a matrix table.
.EXAMPLE
PS C:\> .\Get-MailboxPermissionMatrix.ps1 -OutputFile "C:\output.csv"
Outputs the CSV file to the specified location.
.INPUTS
None. You cannot pipe objects to Get-MailboxPermissionMatrix.ps1.
.OUTPUTS
Returns an array of pscustomobjects if no parameters have been used.
Outputs a CSV file if a path has been specified using the OutputFile parameter.
.LINK
GitHub: https://github.com/MichaelSchoenburg/Get-MailboxPermissionMatrix
.NOTES
Author: Michael Schönburg
Version: v1.0
Last Edit: 03.08.2022
This projects code loosely follows the PowerShell Practice and Style guide, as well as Microsofts PowerShell scripting performance considerations.
Style guide: https://poshcode.gitbook.io/powershell-practice-and-style/
Performance Considerations: https://docs.microsoft.com/en-us/powershell/scripting/dev-cross-plat/performance/script-authoring-considerations?view=powershell-7.1
#>
#region INITIALIZATION
<#
Libraries, Modules, ...
#>
param (
[Parameter(
Mandatory = $false,
Position = 0
)]
[System.IO.FileInfo]
$OutputFile
)
#endregion INITIALIZATION
#region DECLARATIONS
<#
Declare local variables and global variables
#>
#endregion DECLARATIONS
#region FUNCTIONS
<#
Declare Functions
#>
function Write-ConsoleLog {
<#
.SYNOPSIS
Logs an event to the console.
.DESCRIPTION
Writes text to the console with the current date (US format) in front of it.
.PARAMETER Text
Event/text to be outputted to the console.
.EXAMPLE
Write-ConsoleLog -Text 'Subscript XYZ called.'
Long form
.EXAMPLE
Log 'Subscript XYZ called.
Short form
#>
[alias('Log')]
[CmdletBinding()]
param (
[Parameter(Mandatory = $true,
Position = 0)]
[string]
$Text
)
# Save current VerbosePreference
$VerbosePreferenceBefore = $VerbosePreference
# Enable verbose output
$VerbosePreference = 'Continue'
# Write verbose output
Write-Verbose "$( Get-Date -Format 'MM/dd/yyyy HH:mm:ss' ) - $( $Text )"
# Restore current VerbosePreference
$VerbosePreference = $VerbosePreferenceBefore
}
#endregion FUNCTIONS
#region EXECUTION
<#
Script entry point
#>
# Requirement: Is the module loaded?
if (-not ( Get-Module ExchangeOnlineManagement )) {
# Requirement: Is the module even installed?
if (-not ( Get-Module -ListAvailable ExchangeOnlineManagement )) {
Write-Warning -Message "Exchange Online Management PowerShell Module not installed. Please install via 'Install-Module ExchangeOnlineManagement'"
}
Write-Warning -Message "Exchange Online Management PowerShell Module not imported. Please import via 'Import-Module ExchangeOnlineManagement'"
# Requirements satisfied
} else {
try {
$Mbxs = (Get-Mailbox).where({$_.Name -notlike 'DiscoverySearchMailbox*'})
} catch [System.Management.Automation.CommandNotFoundException] {
Write-Warning -Message "Exchange Online Management PowerShell session active. Please connect to Exchange Online via 'Connect-ExchangeOnline'"
} catch {
Write-Warning -Message "An unexpected error occurred while getting all mailboxes."
return $_
}
try {
$Columns = @()
foreach ($user in $Mbxs) {
$rows = [PSCustomObject]@{}
$rows | Add-Member -MemberType NoteProperty -Name 'x' -Value $user.PrimarySmtpAddress
$MbxPermissions = Get-MailboxPermission -Identity $user.PrimarySmtpAddress
foreach ($trustee in $Mbxs) {
if ($MbxPermissions.User -contains $trustee.PrimarySmtpAddress) {
$rows | Add-Member -MemberType NoteProperty -Name $trustee.PrimarySmtpAddress -Value 'x'
} else {
$rows | Add-Member -MemberType NoteProperty -Name $trustee.PrimarySmtpAddress -Value '-'
}
}
$Columns += $rows
}
}
catch {
Write-Warning -Message "An error occured during processing."
return $_
}
try {
if ($OutputFile) {
$Columns | Export-Csv -NoTypeInformation -Path $OutputFile
Write-ConsoleLog "CSV file saved at $( $OutputFile )"
} else {
return $Columns
}
}
catch {
Write-Warning -Message "An error occured during final output."
return $_
}
}
#endregion EXECUTION