-
Notifications
You must be signed in to change notification settings - Fork 585
/
Find-AuditEventsForUser.PS1
317 lines (304 loc) · 12.7 KB
/
Find-AuditEventsForUser.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# Find-AuditEventsForUser.PS1
# Find and report audit events for actions taken by a user over a specified period.
# Demo for ESPC 2024 in Stockholm
# V1.0 28-Nov-2024
# GitHub Link: https://github.com/12Knocksinna/Office365itpros/blob/master/Find-AuditEventsForUser.PS1
[array]$Modules = Get-Module | Select-Object -ExpandProperty Name
If ($Modules -notcontains "ExchangeOnlineManagement") {
Write-Host "Connecting to Exchange Online..."
Connect-ExchangeOnline -ShowBanner:$False
}
$User = Read-Host "Enter the user's UPN"
$StartDate = Read-Host "Enter the start date (dd-mm-yyyy)"
If (!(Get-ExoMailbox -Identity $User)) {
Write-Host "User $User not found"
Exit
}
$EndDate = (Get-Date $StartDate).AddDays(1)
Write-Host ("Searching the audit log for actions for {0} between {1} and {2}" -f $User, $StartDate, (Get-Date $EndDate -format 'dd-MMM-yyyy'))
[array]$Records = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -UserIds $User -ResultSize 5000 -SessionCommand ReturnLargeSet
If ($Records.Count -eq 0) {
Write-Host "No audit records found for $User between $StartDate and $EndDate"
Exit
}
$Records = $Records | Sort-Object Identity -Unique
$Records = $Records | Sort-Object {$_.CreationDate -as [datetime]}
Write-Host ("Found {0} audit records for {1}. First record from {2}. Last record at {3}" -f $Records.Count, $User, $Records[0].CreationDate, $Records[-1].CreationDate)
# Overall summary
$RecordsSummary = $Records | Group-Object Operations -NoElement | Sort-Object Count -Descending
Write-Host "Summary of actions taken by $User"
$RecordsSummary | Format-Table Name, Count -AutoSize
# Remove UserLoggedIn events
[array]$Records1 = $Records | Where-Object {$_.Operations -ne "UserLoggedIn"}
$Report = [System.Collections.Generic.List[Object]]::new()
Write-Host ("Processing {0} audit records for {1} between {2} and {3}" -f $Records1.Count, $User, $StartDate, $EndDate)
# Process each audit record and attempt to make sense of the audit data payload
ForEach ($Rec in $Records1) {
$Action = $null; $Object = $null; $Location = $null; $Workload = $null
$AuditData = $Rec.AuditData | ConvertFrom-Json
Switch ($Rec.Operations) {
"Create" {
$Action = 'Item created in mailbox'
$Object = $AuditData.Item.Subject
$Location = ($AuditData.MailboxOwnerUPN + $AuditData.Item.ParentFolder.Path)
$Workload = "Exchange Online"
}
"CopilotInteraction" {
$Action = 'Copilot interaction'
$Object = $AuditData.CopilotEventData.ThreadId
$Location = $AuditData.CopilotEventData.AppHost
$Workload = "Copilot"
}
"FileAccessed" {
$Action = 'File accessed'
$Object = $AuditData.SourceFileName
$Location = $AuditData.ObjectId
$Workload = "SharePoint Online"
}
"FileModified" {
$Action = 'File modified'
$Object = $AuditData.SourceFileName
$Location = $AuditData.ObjectId
$Workload = "SharePoint Online"
}
"FileRenamed" {
$Action = 'File renamed'
$Object = $AuditData.SourceFileName
$Location = $AuditData.ObjectId
$Workload = "SharePoint Online"
}
"FolderCreated" {
$Action = 'Folder created'
$Object = $AuditData.SourceFileName
$Location = $AuditData.ObjectId
$Workload = "SharePoint Online"
}
"FolderModified" {
$Action = 'Folder modified'
$Object = $AuditData.SourceFileName
$Location = $AuditData.ObjectId
$Workload = "SharePoint Online"
}
"FileModifiedExtended" {
$Action = 'File modified'
$Object = $AuditData.SourceFileName
$Location = $AuditData.ObjectId
$Workload = "SharePoint Online"
}
"FileSensitivityLabelApplied" {
$Action = 'Sensitivity label applied to file'
$Object = $AuditData.SourceFileName
$Location = $AuditData.ObjectId
$Workload = "SharePoint Online"
}
"MailItemsAccessed" {
$Action = 'Mail item accessed'
If ($AuditData[0].Item.ParentFolder.Name) {
$Object = $AuditData[0].Item.ParentFolder.Name
} Else {
$Object = 'Message Bind'
}
$Location = ($AuditData[0].MailboxOwnerUPN + $AuditData.Item.ParentFolder.Path)
$Workload = "Exchange Online"
}
"MoveToDeletedItems" {
$Action = 'Item moved to Deleted Items'
$Object = $Auditdata[0].AffectedItems.Subject
$Location = ($AuditData[0].MailboxOwnerUPN + $AuditData[0].AffectedItems.ParentFolder.Path)
$Workload = "Exchange Online"
}
"HardDelete" {
$Action = 'Item purged from mailbox'
$Object = $Auditdata.AffectedItems[0].Subject
$Location = ($AuditData.MailboxOwnerUPN + $AuditData.AffectedItems[0].ParentFolder.Path)
$Workload = "Exchange Online"
}
"SoftDelete" {
$Action = 'Item removed to Recoverable Items'
$Object = $Auditdata.AffectedItems[0].Subject
$Location = ($AuditData.MailboxOwnerUPN + $AuditData.AffectedItems[0].ParentFolder.Path)
$Workload = "Exchange Online"
}
"Send" {
$Action = 'Message Sent'
$Object = $AuditData[0].Item.Subject
$Location = ($AuditData[0].MailboxOwnerUPN + $AuditData[0].Item.ParentFolder.Path)
$Workload = "Exchange Online"
}
"SendAs" {
$Action = 'Message sent as another user'
$Object = $AuditData[0].Item.Subject
$Location = ($AuditData[0].MailboxOwnerUPN + $AuditData[0].Item.ParentFolder.Path)
$Workload = "Exchange Online"
}
"MessageSent" {
$Action = 'Teams message sent'
$Object = $AuditData.MessageId
$Location = ("Posted to {0} in {1}" -f $AuditData.ChannelName, $AuditData.TeamName)
$Workload = "Microsoft Teams"
}
"MessageCreatedHasLink" {
$Action = 'Teams message created with link'
$Object = $AuditData.MessageId
$Location = ("Posted to {0} in {1}" -f $AuditData.ChannelName, $AuditData.TeamName)
$Workload = "Microsoft Teams"
}
"TaskCreated" {
$Action = 'Task created'
$Object = $AuditData.ObjectId
$Location = $AuditData.Workload
$Workload = "Planner"
}
"TaskModified" {
$Action = 'Task modified'
$Object = $AuditData.ObjectId
$Location = $AuditData.Workload
$Workload = "Planner"
}
"TaskRead" {
$Action = 'Task read'
$Object = $AuditData.ObjectId
$Location = $AuditData.Workload
$Workload = "Planner"
}
"TaskListRead" {
$Action = 'Task list read'
$Object = $AuditData.ObjectId
$Location = $AuditData.Workload
$Workload = "Planner"
}
"PlanRead" {
$Action = 'Plan read'
$Object = $AuditData.ObjectId
$Location = $AuditData.Workload
$Workload = "Planner"
}
"NewRetentionCompliancePolicy" {
$Action = 'Retention policy created'
$Object = $Auditdata.Parameters.Value.Split("-A")[0].Split('"')[1]
$Location = $AuditData.Workload
$Workload = "Data lifecycle management"
}
"NewRetentionComplianceRule" {
$Action = 'Retention rule created'
$Object = $Auditdata.Parameters.value[0].Split("-Expiration")[1]
$Location = $Auditdata.Parameters.Value.Split("-A")[0].Split('"')[1]
$Workload = "Data lifecycle management"
}
"Get-ComplianceSearch" {
$Action = 'Compliance search retrieved'
If ($AuditData.Parameters -eq '-ResultSize "Unlimited"' ) {
$Object = "All results"
} Else {
Try {
$Object = $AuditData.Parameters.Split('"')[1]
} Catch {
$Object = "Unknown"
}
}
$Location = $AuditData.Workload
$Workload = 'eDiscovery'
}
"Set-ComplianceSearch" {
$Action = 'Compliance search updated'
$Object = $Auditdata.Parameters.Split("-D")[0].Split('"')[1]
$Location = $AuditData.Workload
$Workload = 'eDiscovery'
}
"Start-ComplianceSearch" {
$Action = 'Compliance search started'
$Object = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String( $AuditData.Parameters.Split('"')[1].SubString(0,32)))
$Location = $AuditData.Workload
$Workload = 'eDiscovery'
}
"New-ComplianceSearchAction" {
$Action = 'Compliance search action created'
$Object = $AuditData.Parameters
$Location = $AuditData.Workload
$Workload = 'eDiscovery'
}
"Get-ComplianceCase" {
$Action = 'Compliance case retrieved'
$Object = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String( $AuditData.Parameters.Split('"')[1].SubString(0,48)))
$Location = $AuditData.Workload
$Workload = "eDiscovery"
}
"Get-ComplianceCaseMember" {
$Action = 'Compliance case membership retrieved'
$Object = $AuditData.Parameters
$Location = $AuditData.Workload
$Workload = "eDiscovery"
}
"Set-Mailbox" {
$Action = 'Mailbox settings updated'
$Object = $AuditData.Parameters.value[0]
$Location = $AuditData.Workload
$Workload = 'Exchange Online PowerShell'
}
"Search" {
$Action = 'Search performed'
$Object = $AuditData.DataType
$Location = $AuditData.DatabaseType
$Workload = "Data Insights"
}
"SearchQueryInitiatedSharePoint" {
$Action = 'SharePoint Search performed'
$Object = $AuditData.QueryText
$Location = $AuditData.ScenarioName
$Workload = "SharePoint Online"
}
"SearchQueryInitiatedExchange" {
$Action = 'Exchange Search performed'
$Object = $AuditData.QueryText
$Location = $AuditData.ScenarioName
$Workload = "Exchange Online"
}
"SearchViewed" {
$Action = 'Search results viewed'
$Object = $AuditData.ObjectId
$Location = $AuditData.Query
$Workload = "eDiscovery"
}
"QueryUpdate" {
$Action = 'Search query updated'
$Object = $AuditData.QueryText
$Location = $AuditData.CaseId
$Workload = "eDiscovery"
}
"Validate" {
$Action = 'Search query validated'
$Object = $AuditData.DataType
$Location = $AuditData.RelativeURL
$Workload = "eDiscovery"
}
"GATFRTokenIssue" {
$Action = 'Get Access Token for Resource'
$Object = $AuditData.ResourceURL
$Location = "Outlook Web Access"
$Workload = "Exchange Online"
}
Default {
$Action = $Rec.Operations
$Object = $AuditData.ObjectId
$Location = $AuditData.Workload
$Workload = $AuditData.Workload
}
}
$ReportLine = [pscustomobject]@{
CreationDate = $Rec.CreationDate
Operation = $Rec.Operations
User = $Rec.UserIds
Record = $Rec.RecordType
Action = $Action
Object = $Object
Location = $Location
Workload = $Workload
}
$Report.Add($ReportLine)
}
# Show what we've done...
$Report | Out-GridView -Title "Audit events for $User between $StartDate and $EndDate"
# An example script used to illustrate a concept. More information about the topic can be found in the Office 365 for IT Pros eBook https://gum.co/O365IT/
# and/or a relevant article on https://office365itpros.com or https://www.practical365.com. See our post about the Office 365 for IT Pros repository # https://office365itpros.com/office-365-github-repository/ for information about the scripts we write.
# Do not use our scripts in production until you are satisfied that the code meets the need of your organization. Never run any code downloaded from the Internet without
# first validating the code in a non-production environment.