-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-MailboxReport_2024.ps1
226 lines (179 loc) · 9.43 KB
/
Get-MailboxReport_2024.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
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
<#
.Synopsis
Mailbox report merging output properties from multiple cmdlets into one common output object.
.Description
Pulls together some essential details for all on-premises-sourced mailboxes, including migrated (with fewer
details), and excluding Arbitration (i.e. System) and Discovery mailboxes.
Sources include:
- Get-Recipient
- Get-Mailbox
- Get-User
- Get-ADUser
- Get-MailboxStatistics
- Get-MobileDeviceStatistics
Requires an open PSSession to an on-premises Exchange server(2016+).
#>
#Requires -Version 5.1
#Requires -Modules ActiveDirectory
[CmdletBinding()]
param(
[ValidatePattern('^\w+([-+.'']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$')]
[string]$AadUPN
)
begin {
# Use Exchange 2016 Management Shell or remote PowerShell from Windows 10 to an Exchange 2016 server.
$PSSessionsByComputerName = Get-PSSession | Group-Object -Property ComputerName
if (-not (Get-Command Get-MobileDeviceStatistics)) {
Write-Warning -Message "Command 'Get-MobileDeviceStatistics' is not available. Make sure to run this script against Exchange 2016 or newer."
break
}
elseif ($PSSessionsByComputerName.Name -eq 'outlook.office365.com') {
Write-Warning -Message 'EXO PSSession detected. This script is intended for use with on-premises Exchange (and from an AD-joined computer). Exiting script.'
break
}
# Use a domain-joined computer.
if ((Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain -eq $false) {
Write-Warning -Message 'This script must be run from a domain-joined computer. Exiting script.'
break
}
$Start = [datetime]::Now
$ProgressProps = @{
Activity = "Get-MailboxReport - Start time: $($Start)"
Status = 'Working'
PercentComplete = -1
}
try {
Write-Progress @ProgressProps -CurrentOperation 'Get-Mailbox (on-premises mailboxes)'
$LocalMailboxes = Get-Mailbox -ResultSize Unlimited -ErrorAction Stop |
Where-Object { $_.RecipientTypeDetails -ne 'DiscoveryMailbox' -and $_.RecipientTypeDetails -ne 'ArbitrationMailbox' }
Write-Progress @ProgressProps -CurrentOperation 'Get-Recipient -ResultSize Unlimited (mailboxes local/remote)'
$MailboxRecipients = Get-Recipient -ResultSize Unlimited -ErrorAction Stop |
Where-Object { $_.RecipientTypeDetails -match '(^(User)|(Shared)|(Room)|(Equipment)|(Remote).*Mailbox$)' }
Write-Progress @ProgressProps -CurrentOperation 'Get-User -ResultSize Unlimited (mailboxes local/remote)'
$MailboxUsers = Get-User -ResultSize Unlimited -ErrorAction Stop |
Where-Object { $_.RecipientTypeDetails -match '(^(User)|(Shared)|(Room)|(Equipment)|(Remote).*Mailbox$)' }
Write-Progress @ProgressProps -CurrentOperation 'Get-ADUser (mailboxes local/remote)'
$ADMailboxUsers = Get-ADUser -Filter "msExchMailboxGuid -like '*'" -Properties msExchMailboxGuid, LastLogonDate -ErrorAction Stop
}
catch {
Write-Warning -Message "Failed on initial data collection step. Exiting script. Error`n`n($_.Exception)"
break
}
}
process {
# Prepare lookup tables:
Write-Progress @ProgressProps -CurrentOperation 'Preparing lookup tables'
$lmHT = @{}
foreach ($lm in $LocalMailboxes) {
$lmHT[$lm.Guid.Guid] = $lm
}
$muHT = @{}
foreach ($mu in $MailboxUsers) {
$muHT[$mu.Guid.Guid] = $mu
}
$admuHT = @{}
foreach ($admu in $ADMailboxUsers) {
$admuHT[$admu.ObjectGuid.Guid] = $admu
}
# Start the main loop:
$ProgressCounter = 0
$Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
foreach ($mr in $MailboxRecipients) {
$ProgressCounter++
if ($Stopwatch.Elapsed.Milliseconds -ge 300) {
$ProgressProps['PercentComplete'] = (($ProgressCounter / $MailboxRecipients.Count) * 100)
$ProgressProps['CurrentOperation'] = "Preparing common output object for $($mr.DisplayName) ($($mr.RecipientTypeDetails))"
Write-Progress @ProgressProps
$Stopwatch.Restart()
}
# Start building the commonized object for this user, using properties available from the initial Get- cmdlets earlier:
$mrHT = [ordered]@{
DisplayName = $mr.DisplayName
AccountEnabled = $admuHT[$mr.Guid.Guid].Enabled
ADLastLogonDate = if ($admuHT[$mr.Guid.Guid].LastLogonDate) { $admuHT[$mr.Guid.Guid].LastLogonDate.ToString('yyyy-MM-dd') } else { '' }
FirstName = $mr.FirstName
Initials = $muHT[$mr.Guid.Guid].Initials
LastName = $mr.LastName
MobilePhone = $muHT[$mr.Guid.Guid].MobilePhone
Phone = $mr.Phone
PrimarySmtpAddress = $mr.PrimarySmtpAddress
UserPrincipalName = $muHT[$mr.Guid.Guid].UserPrincipalName
PSmtpUpnMatch = if ($mr.PrimarySmtpAddress -eq $muHT[$mr.Guid.Guid].UserPrincipalName) { $true } else { $false }
RecipientTypeDetails = $mr.RecipientTypeDetails
EmailAddressPoliciesEnabled = $mr.EmailAddressPolicyEnabled
RemoteRoutingAddress = ''
HiddenFromAddressListsEnabled = $mr.HiddenFromAddressListsEnabled
Database = $mr.Database
MailboxSizeGB = ''
MailboxItemCount = ''
NewestSentItem = ''
ArchiveState = $mr.ArchiveState
ArchiveDatabase = $mr.ArchiveDatabase
ArchiveSizeGB = ''
ArchiveItemCount = ''
DevicesCount = ''
MostRecentDeviceSuccessSync = ''
MostRecentDeviceType = ''
MostRecentDeviceId = ''
Office = $mr.Office
ManagerId = $mr.Manager
Title = $mr.Title
Department = $mr.Department
Company = $mr.Company
Guid = $mr.Guid
ExchangeGuid = $mr.ExchangeGuid
ArchiveGuid = $mr.ArchiveGuid
OrganizationalUnit = $mr.OrganizationalUnit
CanonicalName = $mr.Identity
EmailAddresses = $mr.EmailAddresses -join ' | '
}
$RemoteRoutingAddress = @()
$RemoteRoutingAddress += $mr.EmailAddresses | Where-Object { $_ -like 'smtp:*@*.mail.onmicrosoft.com' }
$mrHT['RemoteRoutingAddress'] = $RemoteRoutingAddress[0] -replace 'smtp:'
if ($mr.RecipientTypeDetails -notmatch '(^Remote.*)') {
# Only processing local mailboxes (we're not processing remote/migrated mailboxes).
$MStats = $null
$MStats = $mr | Get-MailboxStatistics -ErrorAction Continue
$MailboxSizeGB = try {
[math]::Round( ([decimal]($MStats.TotalItemSize -replace '(.*\()|(,)|(\s.*)') + [decimal]($MStats.TotalDeletedItemSize -replace '(.*\()|(,)|(\s.*)')) / 1GB, 2 )
}
catch { '' }
$MFSIStats = Get-MailboxFolderStatistics -Identity $mr.Guid.Guid -FolderScope SentItems -IncludeOldestAndNewestItems |
Sort-Object { $_.Identity -match '(Sent Items$)' }
$MStatsArchive = $null
if ($lmHT[$mr.Guid.Guid].ArchiveState -like 'Local') {
$MStatsArchive = $mr | Get-MailboxStatistics -Archive -ErrorAction Continue
}
if ($MStatsArchive) {
$ArchiveSizeGB = try {
[math]::Round( ([decimal]($MStatsArchive.TotalItemSize -replace '(.*\()|(,)|(\s.*)') + [decimal]($MStatsArchive.TotalDeletedItemSize -replace '(.*\()|(,)|(\s.*)')) / 1GB, 2 )
}
catch { '' }
$ArchiveItemCount = $MStatsArchive.ItemCount
}
else {
$ArchiveSizeGB = ''
$ArchiveItemCount = ''
}
$MDevs = @()
$MDevs += Get-MobileDeviceStatistics -Mailbox $mr.Guid.Guid -ErrorAction Continue
$RecentMDev = $null
$RecentMDev = $MDevs | Sort-Object -Property LastSuccessSync | Select-Object -Last 1
# Add the on-premises mailbox-related properties to the output object:
$mrHT['MailboxSizeGB'] = $MailboxSizeGB
$mrHT['MailboxItemCount'] = $MStats.ItemCount
$mrHT['NewestSentItem'] = $MFSIStats.NewestItemReceivedDate
$mrHT['ArchiveState'] = $lmHT[$mr.Guid.Guid].ArchiveState
$mrHT['ArchiveDatabase'] = $lmHT[$mr.Guid.Guid].ArchiveDatabase
$mrHT['ArchiveSizeGB'] = $ArchiveSizeGB
$mrHT['ArchiveItemCount'] = $ArchiveItemCount
$mrHT['DevicesCount'] = $MDevs.Count
$mrHT['MostRecentDeviceSuccessSync'] = $RecentMDev.LastSuccessSync
$mrHT['MostRecentDeviceType'] = $RecentMDev.DeviceType
$mrHT['MostRecentDeviceId'] = $RecentMDev.DeviceId
}
Write-Debug -Message 'Stop to inspect $mrHT, $mr, $admuHT[$mr.Guid.Guid], $muHT[$mr.Guid.Guid].'
# Output the commonized object:
[PSCustomObject]$mrHT
}
}