-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMailUser-MgUser-Activity-Report.ps1
More file actions
111 lines (90 loc) · 3.6 KB
/
MailUser-MgUser-Activity-Report.ps1
File metadata and controls
111 lines (90 loc) · 3.6 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
<#
.SYNOPSIS
Generates a sign-in activity report for Exchange Online mail users via Microsoft Graph.
.DESCRIPTION
Queries Exchange Online for MailUser recipients, then retrieves sign-in activity and
profile details from Microsoft Graph (beta endpoint) for each user. Exports the results
to an Excel file.
Connects to both Exchange Online and Microsoft Graph automatically.
.PARAMETER ExchangeUpn
The UPN to authenticate with for Connect-ExchangeOnline.
.PARAMETER TenantId
The Tenant ID or domain for Connect-MgGraph.
.PARAMETER OutputDirectory
Directory path for the output Excel report. Defaults to 'c:\tmp'.
.EXAMPLE
.\MailUser-MgUser-Activity-Report.ps1 -ExchangeUpn admin@example.com -TenantId 'contoso.onmicrosoft.com'
Connects to Exchange Online and Microsoft Graph, then generates the activity report
in the default output directory (c:\tmp).
.EXAMPLE
.\MailUser-MgUser-Activity-Report.ps1 -ExchangeUpn admin@example.com -TenantId 'contoso.onmicrosoft.com' -OutputDirectory 'C:\Reports'
Same report with a custom output directory.
.NOTES
Author: Mike Crowley
https://mikecrowley.us
Requires: ExchangeOnlineManagement, Microsoft.Graph.Users, ImportExcel modules
Permissions: User.Read.All (Graph beta)
.LINK
https://github.com/Mike-Crowley/Public-Scripts
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$ExchangeUpn,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$TenantId,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$OutputDirectory = 'c:\tmp'
)
Connect-ExchangeOnline -UserPrincipalName $ExchangeUpn
Connect-MgGraph -TenantId $TenantId
Select-MgProfile beta
$MailUsers = Get-MailUser -filter { recipienttypedetails -eq 'MailUser' } -ResultSize unlimited
$Counter = 0
$ReportUsers = $MailUsers | ForEach-Object {
$MailUser = $_
$Counter++
$percentComplete = (($Counter / $MailUsers.count) * 100)
Write-Progress -Activity "Getting MG Objects" -PercentComplete $percentComplete -Status "$percentComplete% Complete:"
#to do - organize properties better
Get-MgUser -UserId $MailUser.ExternalDirectoryObjectId -ConsistencyLevel eventual -Property @(
'UserPrincipalName'
'SignInActivity'
'CreatedDateTime'
'DisplayName'
'Mail'
'OnPremisesImmutableId'
'OnPremisesDistinguishedName'
'OnPremisesLastSyncDateTime'
'SignInSessionsValidFromDateTime'
'RefreshTokensValidFromDateTime'
'id'
) | Select-Object @(
'UserPrincipalName'
'CreatedDateTime'
'DisplayName'
'Mail'
'OnPremisesImmutableId'
'OnPremisesDistinguishedName'
'OnPremisesLastSyncDateTime'
'SignInSessionsValidFromDateTime'
'RefreshTokensValidFromDateTime'
'id'
@{n = 'PrimarySmtpAddress'; e = { $MailUser.PrimarySmtpAddress } }
@{n = 'ExternalEmailAddress'; e = { $MailUser.ExternalEmailAddress } }
@{n = 'LastSignInDateTime'; e = { [datetime]$_.SignInActivity.LastSignInDateTime } }
@{n = 'lastNonInteractiveSignInDateTime'; e = { [datetime]$_.SignInActivity.AdditionalProperties.lastNonInteractiveSignInDateTime } }
)
}
$Common_ExportExcelParams = @{
# PassThru = $true
BoldTopRow = $true
AutoSize = $true
AutoFilter = $true
FreezeTopRow = $true
}
$FileDate = Get-Date -Format yyyyMMddTHHmmss
$ReportUsers | Sort-Object UserPrincipalName | Export-Excel @Common_ExportExcelParams -Path ("$OutputDirectory\" + $filedate + "_report.xlsx") -WorksheetName report