-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-EXOMDevStats.ps1
94 lines (80 loc) · 2.75 KB
/
Get-EXOMDevStats.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
<#
.Synopsis
Get mobile devices and some of their pertinent details/statistics for all mailboxes in the currently connected EXO tenant.
.Example
.\Get-EXOMDevStats.ps1 | Export-Csv "EXOMDevStats_$(Get-Date -Format 'yyyy-MM-dd').csv" -NTI -Encoding UTF8
#>
#Requires -Version 5.1
#Requires -Modules ExchangeOnlineManagement
[CmdletBinding()]
param()
#region Initialization
$PSSessionsByComputerName = Get-PSSession | Group-Object -Property ComputerName
if (-not (Get-Command Get-MobileDeviceStatistics)) {
"Command 'Get-EXOMobileDeviceStatistics' is not available. Make sure to run this script against Exchange Online using the EXOv2 module " +
"(Install-Module ExchangeOnlineManagement -Scope CurrentUser; Connect-ExchangeOnline). Exiting script." | Write-Warning
break
}
elseif (-not ($PSSessionsByComputerName.Name -eq 'outlook.office365.com')) {
Write-Warning -Message 'No EXO PSSession detected. Connect first using Connect-ExchangeOnline. Exiting script.'
break
}
$Start = [datetime]::Now
$ProgressProps = @{
Activity = "Get-EXOMDevStats.ps1 - Start time: $($Start)"
Status = 'Working'
PercentComplete = -1
}
try {
Write-Progress @ProgressProps -CurrentOperation 'Get-EXOMailbox (ExchangeOnlineManagement module)'
$EXOMailboxes = Get-EXOMailbox -ResultSize Unlimited -ErrorAction Stop
}
catch {
Write-Warning -Message "Failed on initial Get-EXOMailbox step. Exiting script. Error`n`n($_.Exception)"
break
}
#endregion Initialization
#region Main loop
$ProgressCounter = 0
$Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
foreach ($mbx in $EXOMailboxes) {
$ProgressCounter++
if ($Stopwatch.Elapsed.Milliseconds -ge 300) {
$ProgressProps['PercentComplete'] = (($ProgressCounter / $EXOMailboxes.Count) * 100)
$ProgressProps['CurrentOperation'] = "Preparing user/device combined objects for $($mbx.DisplayName) ($($mbx.PrimarySmtpAddress))"
Write-Progress @ProgressProps
$Stopwatch.Restart()
}
$MDevs = @()
$MDevs += Get-EXOMobileDeviceStatistics -Mailbox $mbx.Guid.Guid -ErrorAction SilentlyContinue |
Select-Object -Property @{
Name = 'UserDisplayName'
Expression = { $mbx.DisplayName }
},
LastSuccessSync,
DeviceAccessState,
DeviceAccessStateReason,
Status,
DeviceModel,
DeviceImei,
DevicePhoneNumber,
DeviceOS,
DeviceType,
DeviceID,
DeviceUserAgent,
DeviceFriendlyName,
DeviceMobileOperator,
DevicePolicyApplied,
DevicePolicyApplicationStatus,
LastDeviceWipeRequestor,
ClientVersion,
NumberOfFoldersSynced,
ClientType,
Guid,
@{
Name = 'UserUPN'
Expression = {$mbx.UserPrincipalName}
}
$MDevs
}
#region Main loop