-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-SharedMailboxesMobileAccessReport.ps1
163 lines (138 loc) · 7.37 KB
/
Get-SharedMailboxesMobileAccessReport.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
<#
.SYNOPSIS
Exchange Server 2019 Shared Mailboxes Mobile Access Report
.DESCRIPTION
This PowerShell script retrieves all shared mailboxes in an Exchange Server 2019 environment, checks if ActiveSync is enabled for each mailbox, identifies any mobile devices accessing them, gathers detailed statistics for each device, and exports the information into two separate CSV files:
1. SharedMailboxesReport.csv - Contains mailbox-level information.
2. MobileDevicesReport.csv - Contains device-level information with references to their associated mailboxes.
.PREREQUISITES
- Exchange Server 2019
- Administrative permissions to execute Exchange cmdlets
- Exchange Management Shell or imported Exchange module in PowerShell
- PowerShell 5.1 or later
.USAGE
1. Open the Exchange Management Shell with administrative privileges.
2. Copy and paste the script into the shell or save it as `Get-SharedMailboxesMobileAccessReport.ps1` and execute it by navigating to its directory and running:
.\Get-SharedMailboxesMobileAccessReport.ps1
3. The reports will be generated at the specified export paths:
- SharedMailboxesReport.csv
- MobileDevicesReport.csv
.OUTPUTS
- CSV files containing detailed reports on shared mailboxes and their associated mobile devices.
.EXAMPLE
.\Get-SharedMailboxesMobileAccessReport.ps1
.NOTES
- Ensure that the executing account has the necessary permissions for both `Get-MobileDevice` and `Get-MobileDeviceStatistics` cmdlets.
- Modify the `$mailboxesExportPath` and `$devicesExportPath` variables as needed to specify different export locations.
#>
# Exchange Server 2019 PowerShell Script
# Description: Retrieves shared mailboxes, checks if ActiveSync is enabled,
# identifies mobile devices accessing them, retrieves detailed statistics,
# and exports the information to separate CSV files for mailboxes and devices.
# Define the output CSV file paths
$mailboxesExportPath = "C:\Reports\SharedMailboxesReport.csv"
$devicesExportPath = "C:\Reports\MobileDevicesReport.csv"
# Ensure the export directory exists; create it if it doesn't
$exportDirectory = Split-Path -Path $mailboxesExportPath
if (!(Test-Path -Path $exportDirectory)) {
New-Item -Path $exportDirectory -ItemType Directory -Force | Out-Null
}
# Retrieve all shared mailboxes
$sharedMailboxes = Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited
# Initialize arrays to store the report data
$mailboxesReport = @()
$devicesReport = @()
# Iterate through each shared mailbox
foreach ($mailbox in $sharedMailboxes) {
try {
# Get Client Access settings for the mailbox
$casMailbox = Get-CASMailbox -Identity $mailbox.Identity
# Check if ActiveSync is enabled
$activeSyncEnabled = $casMailbox.ActiveSyncEnabled
# Initialize variables for mobile device information
$mobileDeviceCount = 0
if ($activeSyncEnabled) {
try {
# Retrieve mobile devices associated with the mailbox
$mobileDevices = Get-MobileDevice -Mailbox $mailbox.Identity -ErrorAction Stop
if ($mobileDevices) {
$mobileDeviceCount = $mobileDevices.Count
foreach ($device in $mobileDevices) {
try {
# Retrieve statistics for each mobile device
$deviceStats = Get-MobileDeviceStatistics -Identity $device.Identity -ErrorAction Stop
# Create a custom object with desired properties
$deviceDetail = [PSCustomObject]@{
MailboxName = $mailbox.DisplayName
MailboxIdentity = $mailbox.Identity
DeviceName = $device.DeviceName
DeviceType = $device.DeviceType
DeviceOS = $device.DeviceOS
LastSuccessSync = $deviceStats.LastSuccessSync
LastSyncAttemptTime = $deviceStats.LastSyncAttemptTime
LastPolicyUpdate = $deviceStats.LastPolicyUpdate
LastAccessTime = $deviceStats.LastAccessTime
DeviceUserAgent = $deviceStats.UserAgent
DeviceAccessState = $deviceStats.DeviceAccessState
DeviceAccessStateReason = $deviceStats.DeviceAccessStateReason
}
# Add the device detail to the devices report array
$devicesReport += $deviceDetail
}
catch {
# Handle errors when retrieving device statistics
$deviceDetail = [PSCustomObject]@{
MailboxName = $mailbox.DisplayName
MailboxIdentity = $mailbox.Identity
DeviceName = $device.DeviceName
DeviceType = $device.DeviceType
DeviceOS = $device.DeviceOS
LastSuccessSync = "Error: $_"
LastSyncAttemptTime = "Error: $_"
LastPolicyUpdate = "Error: $_"
LastAccessTime = "Error: $_"
DeviceUserAgent = "Error: $_"
DeviceAccessState = "Error: $_"
DeviceAccessStateReason = "Error: $_"
}
$devicesReport += $deviceDetail
}
}
}
}
catch {
# Handle cases where Get-MobileDevice might fail
Write-Warning "Error retrieving mobile devices for mailbox '$($mailbox.Identity)': $_"
}
}
# Create a custom object with the desired mailbox properties
$mailboxReportObject = [PSCustomObject]@{
MailboxName = $mailbox.DisplayName
MailboxIdentity = $mailbox.Identity
ActiveSyncEnabled = $activeSyncEnabled
MobileDeviceCount = $mobileDeviceCount
}
# Add the mailbox object to the mailboxes report array
$mailboxesReport += $mailboxReportObject
}
catch {
# Handle any errors encountered while processing the mailbox
Write-Warning "Failed to process mailbox '$($mailbox.Identity)': $_"
}
}
# Export the mailboxes report to a CSV file
try {
$mailboxesReport | Export-Csv -Path $mailboxesExportPath -NoTypeInformation -Encoding UTF8
Write-Output "Mailboxes report successfully exported to '$mailboxesExportPath'."
}
catch {
Write-Error "Failed to export mailboxes report to CSV: $_"
}
# Export the devices report to a CSV file
try {
$devicesReport | Export-Csv -Path $devicesExportPath -NoTypeInformation -Encoding UTF8
Write-Output "Mobile devices report successfully exported to '$devicesExportPath'."
}
catch {
Write-Error "Failed to export mobile devices report to CSV: $_"
}