-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathExport-M365CopilotReports.ps1
More file actions
1343 lines (1133 loc) · 56.4 KB
/
Export-M365CopilotReports.ps1
File metadata and controls
1343 lines (1133 loc) · 56.4 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Enhanced Export logs for Copilot Analytics Reporting, including Entra Users, Purview Audit Logs, etc.
# This script can be used for the Copilot Purview Audit Dashboard, as well as, be used to generate an export of Entra users that can then be used to build an Organizational Data file for Copilot Analytics.
# Features:
# - Export Entra Users Details including Manager Information (Can be used for Org Data Preparation)
# - Export Copilot Audit Logs (with filtering for Copilot interactions)
# - Export Purview Audit Logs (with Custom Operations filtering)
# - Export Microsoft 365 Copilot Usage Reports
# - Extensible for future export functions
# - Interactive startup menu
# Author: Alejandro Lopez | alejandro.lopez@microsoft.com
# Version: v20250924
# Global configuration
$script:Config = @{
DefaultOutputDirectory = $PWD.Path
LogDirectory = $PWD.Path
SessionActive = $false
MGGraphSession = $null
ExchangeSession = $null
}
# Function to create consistent output path
function Get-OutputPath {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string]$BaseName,
[Parameter(Mandatory = $false)]
[string]$Extension = "csv",
[Parameter(Mandatory = $false)]
[string]$CustomPath,
[Parameter(Mandatory = $false)]
[switch]$CreateDirectory
)
# Use custom path if provided, otherwise use default directory
$outputDir = if ([string]::IsNullOrWhiteSpace($CustomPath)) {
$script:Config.DefaultOutputDirectory
} else {
$CustomPath
}
# Create directory if it doesn't exist
if ($CreateDirectory -and -not (Test-Path -Path $outputDir -PathType Container)) {
New-Item -Path $outputDir -ItemType Directory -Force | Out-Null
Write-Host "Created output directory: $outputDir" -ForegroundColor Green
}
# Generate filename with timestamp
$timestamp = Get-Date -Format 'yyyyMMdd_HHmmss'
$fileName = if ([string]::IsNullOrWhiteSpace($BaseName)) {
"Export_${timestamp}.${Extension}"
} else {
"${BaseName}_${timestamp}.${Extension}"
}
return Join-Path -Path $outputDir -ChildPath $fileName
}
# Function to get log file path
function Get-LogPath {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string]$BaseName
)
return Get-OutputPath -BaseName "${BaseName}_Log" -Extension "log" -CreateDirectory
}
# Function to connect to Microsoft Graph
function Connect-ToMicrosoftGraph {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[switch]$Force
)
# Check if already connected
if (-not $Force -and $script:Config.MGGraphSession) {
Write-Host "Already connected to Microsoft Graph." -ForegroundColor Green
return $true
}
try {
Write-Host "Connecting to Microsoft Graph..." -ForegroundColor Cyan
# Added Organization.Read.All scope which is required for license information
Connect-MgGraph -Scopes "User.Read.All", "AuditLog.Read.All", "Reports.Read.All", "Organization.Read.All" -ErrorAction Stop
Write-Host "Successfully connected to Microsoft Graph." -ForegroundColor Green
$script:Config.MGGraphSession = $true
return $true
}
catch {
Write-Host "Error connecting to Microsoft Graph: $_" -ForegroundColor Red
return $false
}
}
# Function to connect to Exchange Online (for Purview audit logs)
function Connect-ToExchangeOnline {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[switch]$Force
)
# Check if already connected
if (-not $Force -and $script:Config.ExchangeSession) {
Write-Host "Already connected to Exchange Online." -ForegroundColor Green
return $true
}
try {
Write-Host "Connecting to Exchange Online..." -ForegroundColor Cyan
Connect-ExchangeOnline -ShowBanner:$false -ErrorAction Stop | Out-Null
Write-Host "Successfully connected to Exchange Online." -ForegroundColor Green
$script:Config.ExchangeSession = $true
return $true
}
catch {
Write-Host "Error connecting to Exchange Online: $_" -ForegroundColor Red
return $false
}
}
# Function to disconnect from all services
function Disconnect-AllServices {
[CmdletBinding()]
param()
try {
if ($script:Config.MGGraphSession) {
Disconnect-MgGraph -ErrorAction SilentlyContinue | out-Null
$script:Config.MGGraphSession = $null
Write-Host "Disconnected from Microsoft Graph." -ForegroundColor Cyan
}
if ($script:Config.ExchangeSession) {
Disconnect-ExchangeOnline -Confirm:$false -ErrorAction SilentlyContinue | out-Null
$script:Config.ExchangeSession = $null
Write-Host "Disconnected from Exchange Online." -ForegroundColor Cyan
}
}
catch {
Write-Host "Error during disconnection: $_" -ForegroundColor Yellow
}
}
# Function to export Entra Users details with license information
function Export-EntraUsersDetails {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$OutputPath,
[Parameter(Mandatory = $false)]
[string]$LogPath
)
# Generate consistent output paths
$outputFilePath = if ([string]::IsNullOrWhiteSpace($OutputPath)) {
Get-OutputPath -BaseName "EntraUsersExport" -CreateDirectory
} else {
$OutputPath
}
$logFilePath = if ([string]::IsNullOrWhiteSpace($LogPath)) {
Get-LogPath -BaseName "EntraUsersExport"
} else {
$LogPath
}
# Start logging
Start-Transcript -Path $logFilePath -Append
Write-Host "Starting Entra user export process. Log file: $logFilePath" -ForegroundColor Cyan
if (-not (Connect-ToMicrosoftGraph)) {
Write-Host "Cannot proceed with export. Microsoft Graph connection failed." -ForegroundColor Red
Stop-Transcript
return
}
try {
Write-Host "Retrieving Entra users..." -ForegroundColor Cyan
# First get all users with basic properties
$users = Get-MgUser -All -Property Id, DisplayName, UserPrincipalName, Mail, JobTitle, Department,
CompanyName, AccountEnabled, CreatedDateTime, UserType, MobilePhone, OfficeLocation,
UsageLocation, City, Country, PostalCode, State, EmployeeHireDate, AssignedLicenses
Write-Host "Retrieved $($users.Count) users from Entra directory." -ForegroundColor Green
if ($users.Count -eq 0) {
Write-Host "No users found." -ForegroundColor Yellow
Stop-Transcript
return
}
# Try to get license information differently - use direct API call instead of Get-MgSubscribedSku
Write-Host "Retrieving license SKU information using direct API call..." -ForegroundColor Cyan
try {
# Direct API call to get subscribed SKUs - this avoids the issue with Get-MgSubscribedSku
$licenseSkus = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/subscribedSkus" -ErrorAction Stop
# Create a hashtable for quick SKU lookup
$skuLookup = @{}
foreach ($sku in $licenseSkus.value) {
$skuLookup[$sku.skuId] = $sku.skuPartNumber
}
Write-Host "Retrieved $($licenseSkus.value.Count) license SKUs." -ForegroundColor Green
}
catch {
Write-Host "Could not retrieve license SKUs. Will proceed with just SKU IDs: $_" -ForegroundColor Yellow
# Initialize an empty lookup
$skuLookup = @{}
}
# Initialize progress bar parameters
$progressParams = @{
Activity = "Exporting Entra User Details"
Status = "Processing users"
PercentComplete = 0
}
# Display initial progress bar
Write-Progress @progressParams
# Create an array to store user details
$userDetails = [System.Collections.Generic.List[PSObject]]::new()
$totalUsers = $users.Count
$processedUsers = 0
$managersFound = 0
$managersNotFound = 0
# Process each user and update progress bar
foreach ($user in $users) {
# Update progress bar
$processedUsers++
$progressParams.PercentComplete = ($processedUsers / $totalUsers) * 100
$progressParams.Status = "Processing user $processedUsers of $totalUsers"
Write-Progress @progressParams
# Initialize manager variables
$managerId = $null
$managerName = $null
$managerEmail = $null
# Get manager details using the beta endpoint that returns direct manager
try {
$managerEndpoint = "https://graph.microsoft.com/v1.0/users/$($user.Id)/manager"
$manager = Invoke-MgGraphRequest -Method GET -Uri $managerEndpoint -ErrorAction SilentlyContinue
if ($manager -and $manager.id) {
$managerId = $manager.id
$managerName = $manager.displayName
$managerEmail = $manager.mail
$managersFound++
Write-Verbose "Found manager for user $($user.UserPrincipalName): $managerName"
}
else {
$managersNotFound++
Write-Verbose "No manager found for user $($user.UserPrincipalName)"
}
}
catch {
$managersNotFound++
Write-Verbose "Error retrieving manager for user $($user.UserPrincipalName): $_"
}
# Process licenses directly from the user object
$licenseDetails = @()
if ($user.AssignedLicenses) {
foreach ($license in $user.AssignedLicenses) {
# Convert SKU ID to readable SKU part number using our lookup table
$skuFriendlyName = if ($skuLookup.ContainsKey($license.SkuId)) {
$skuLookup[$license.SkuId]
} else {
$license.SkuId # Fall back to ID if not found in lookup
}
$licenseDetails += $skuFriendlyName
}
}
# Join license details into a semicolon-separated string
$licensesStr = if ($licenseDetails.Count -gt 0) {
$licenseDetails -join ";"
} else {
"" # Empty string if no licenses
}
# Process user details with additional attributes
$userDetails.Add([PSCustomObject]@{
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
Email = $user.Mail
JobTitle = $user.JobTitle
Department = $user.Department
Company = $user.CompanyName
AccountEnabled = $user.AccountEnabled
CreatedDateTime = $user.CreatedDateTime
UserType = $user.UserType
Id = $user.Id
# Additional attributes
MobilePhone = $user.MobilePhone
OfficeLocation = $user.OfficeLocation
UsageLocation = $user.UsageLocation
City = $user.City
Country = $user.Country
PostalCode = $user.PostalCode
State = $user.State
EmployeeHireDate = $user.EmployeeHireDate
ManagerId = $managerId
ManagerName = $managerName
ManagerEmail = $managerEmail
# License information
AssignedLicenses = $licensesStr
LicenseCount = $licenseDetails.Count
})
}
# Update progress for export phase
$progressParams.Status = "Exporting data to CSV file"
$progressParams.PercentComplete = 99
Write-Progress @progressParams
# Export to CSV
$userDetails | Export-Csv -Path $outputFilePath -NoTypeInformation
# Complete progress bar
Write-Progress -Activity "Exporting Entra User Details" -Completed
Write-Host "Export completed successfully. File saved to: $outputFilePath" -ForegroundColor Green
Write-Host "Total users exported: $($userDetails.Count)" -ForegroundColor Green
Write-Host "Users with managers found: $managersFound" -ForegroundColor Green
Write-Host "Users without managers: $managersNotFound" -ForegroundColor Green
}
catch {
Write-Host "Error exporting Entra users: $_" -ForegroundColor Red
}
finally {
Stop-Transcript
}
}
# Function to export Purview audit logs with filtering options
function Export-PurviewAuditLogs {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$OutputPath,
[Parameter(Mandatory = $false)]
[string[]]$Operations = @(),
[Parameter(Mandatory = $false)]
[DateTime]$StartDate = (Get-Date).AddDays(-7),
[Parameter(Mandatory = $false)]
[DateTime]$EndDate = (Get-Date),
[Parameter(Mandatory = $false)]
[int]$ResultSize = 5000,
[Parameter(Mandatory = $false)]
[string]$LogPath
)
# Generate consistent output paths
$outputFilePath = if ([string]::IsNullOrWhiteSpace($OutputPath)) {
Get-OutputPath -BaseName "PurviewAuditLogs" -CreateDirectory
} else {
$OutputPath
}
$logFilePath = if ([string]::IsNullOrWhiteSpace($LogPath)) {
Get-LogPath -BaseName "PurviewAuditLogs"
} else {
$LogPath
}
# Start logging
Start-Transcript -Path $logFilePath -Append
Write-Host "Starting Purview audit logs export process. Log file: $logFilePath" -ForegroundColor Cyan
if (-not (Connect-ToExchangeOnline)) {
Write-Host "Cannot proceed with export. Exchange Online connection failed." -ForegroundColor Red
Stop-Transcript
return
}
try {
# Format dates for the Search-UnifiedAuditLog cmdlet
$startDateStr = $StartDate.ToString("MM/dd/yyyy")
$endDateStr = $EndDate.ToString("MM/dd/yyyy")
Write-Host "Retrieving Purview audit logs for operations: $($Operations -join ', ')" -ForegroundColor Cyan
Write-Host "Time range: $startDateStr to $endDateStr" -ForegroundColor Cyan
# Build operations filter if specified
$params = @{
StartDate = $startDateStr
EndDate = $endDateStr
ResultSize = $ResultSize
}
# Only add Operations parameter if operations were explicitly specified
# This is critical: if Operations is an empty array, we want ALL operations
# Adding an empty Operations parameter will incorrectly filter the results
if ($Operations.Count -gt 0) {
$operationsFilter = $Operations -join ","
$params.Operations = $operationsFilter
Write-Host "Filtering for specific operations: $operationsFilter" -ForegroundColor Yellow
}
else {
Write-Host "No operations filter applied - will retrieve ALL operations" -ForegroundColor Yellow
}
# Initialize progress bar with more descriptive activity
$operationDescription = if ($Operations.Count -gt 0) {
"for $($Operations -join ', ')"
} else {
"for ALL operations"
}
$progressParams = @{
Activity = "Retrieving Purview Audit Logs $operationDescription"
Status = "Initializing search..."
PercentComplete = 5
}
Write-Progress @progressParams
# Retrieve audit logs
Write-Host "Executing Search-UnifiedAuditLog with parameters:" -ForegroundColor Cyan
$params | Format-Table | Out-String | Write-Host
# Update progress for search phase
$progressParams.Status = "Searching for audit logs... (this may take a while for ALL operations)"
$progressParams.PercentComplete = 10
Write-Progress @progressParams
# Start time measurement for the search
$searchStartTime = Get-Date
# Execute the search
$auditLogs = Search-UnifiedAuditLog @params
# Calculate and display search duration
$searchDuration = (Get-Date) - $searchStartTime
Write-Host "Search completed in $($searchDuration.TotalSeconds.ToString("0.00")) seconds." -ForegroundColor Cyan
if ($null -eq $auditLogs -or $auditLogs.Count -eq 0) {
Write-Host "No audit logs found for the specified criteria." -ForegroundColor Yellow
Stop-Transcript
return
}
Write-Host "Retrieved $($auditLogs.Count) audit log entries." -ForegroundColor Green
# Update progress for processing phase
$progressParams.Status = "Processing audit logs"
$progressParams.PercentComplete = 30
Write-Progress @progressParams
Write-Host "Processing $($auditLogs.Count) audit log entries..." -ForegroundColor Cyan
# Process and expand the audit data
$totalLogs = $auditLogs.Count
$processedLogs = [System.Collections.Generic.List[PSObject]]::new($totalLogs)
$processedCount = 0
$errorCount = 0
foreach ($log in $auditLogs) {
$processedCount++
# Update progress - adapt frequency based on total number of logs
# For larger datasets, update less frequently to improve performance
$updateFrequency = [Math]::Max(1, [Math]::Min(100, [Math]::Floor($totalLogs / 100)))
if ($processedCount % $updateFrequency -eq 0 || $processedCount -eq 1 || $processedCount -eq $totalLogs) {
$percentComplete = 30 + (($processedCount / $totalLogs) * 60) # Scale from 30% to 90%
$progressParams.Status = "Processing log $processedCount of $totalLogs [$('{0:P1}' -f ($processedCount/$totalLogs))]"
$progressParams.PercentComplete = $percentComplete
Write-Progress @progressParams
}
$auditData = $null
try {
$auditData = $log.AuditData | ConvertFrom-Json
# Create a custom object with the relevant properties
$processedLog = [PSCustomObject]@{
CreationDate = $log.CreationDate
UserIds = $log.UserIds
Operations = $log.Operations
RecordType = $log.RecordType
Id = $log.Id
Workload = $auditData.Workload
ObjectId = $auditData.ObjectId
UserId = $auditData.UserId
ClientIP = $auditData.ClientIP
UserAgent = $auditData.UserAgent
Operation = $auditData.Operation
ResultStatus = $auditData.ResultStatus
# For Copilot interactions, include specific fields if available
CopilotPrompt = if ($auditData.Operation -eq "CopilotInteraction") { $auditData.CopilotPrompt } else { $null }
CopilotResponse = if ($auditData.Operation -eq "CopilotInteraction") { $auditData.CopilotResponse } else { $null }
CopilotContext = if ($auditData.Operation -eq "CopilotInteraction") { $auditData.CopilotContext } else { $null }
Application = $auditData.Application
AuditData = $log.AuditData # Include the full JSON for reference
}
$processedLogs.Add($processedLog)
}
catch {
$errorCount++
Write-Host "Error parsing AuditData for record: $($log.Id) - $_" -ForegroundColor Yellow
}
}
# Final progress update for export
$progressParams.Status = "Exporting $($processedLogs.Count) records to CSV..."
$progressParams.PercentComplete = 95
Write-Progress @progressParams
# Export the processed data
$processedLogs | Export-Csv -Path $outputFilePath -NoTypeInformation
# Update progress to 100% before completing
$progressParams.Status = "Export complete!"
$progressParams.PercentComplete = 100
Write-Progress @progressParams
# Complete progress bar - use the same activity name as initialized
Write-Progress -Activity $progressParams.Activity -Completed
Write-Host "Export completed successfully. File saved to: $outputFilePath" -ForegroundColor Green
Write-Host "Total records exported: $($processedLogs.Count)" -ForegroundColor Green
if ($errorCount -gt 0) {
Write-Host "Errors encountered during processing: $errorCount" -ForegroundColor Yellow
}
}
catch {
Write-Host "Error exporting Purview audit logs: $_" -ForegroundColor Red
}
finally {
Stop-Transcript
}
}
function Export-CopilotAuditLogs {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[int]$DaysToSearch = 180,
[Parameter(Mandatory = $false)]
[string]$OutputFolder,
[Parameter(Mandatory = $false)]
[string]$OutputFileName,
[Parameter(Mandatory = $false)]
[int]$IntervalDays = 1
)
# Generate consistent output paths with a sensible default
$outputDir = if ([string]::IsNullOrWhiteSpace($OutputFolder)) {
$PSScriptRoot # Default to the script's current directory
} else {
$OutputFolder
}
# Create output folder if it doesn't exist
if (-not (Test-Path -Path $outputDir -PathType Container)) {
New-Item -Path $outputDir -ItemType Directory -Force | Out-Null
Write-Host "Created output folder: $outputDir" -ForegroundColor Green
}
# Generate output filename
$outputFile = if ([string]::IsNullOrWhiteSpace($OutputFileName)) {
Join-Path -Path $outputDir -ChildPath "Copilot_Activities_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv"
} else {
Join-Path -Path $outputDir -ChildPath $OutputFileName
}
$logFile = Join-Path -Path $outputDir -ChildPath "CopilotAuditLog_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt"
# Helper function to handle error messages
function Write-ErrorDetails {
param ([System.Management.Automation.ErrorRecord]$ErrorRecord)
$message = "Error occurred: $($ErrorRecord.Exception.Message)"
Write-Host $message -ForegroundColor Red
Write-Host "ScriptStackTrace: $($ErrorRecord.ScriptStackTrace)" -ForegroundColor Red
Write-LogFile $message
}
# Helper function to write to the log file
function Write-LogFile ([String]$Message) {
$final = [DateTime]::Now.ToUniversalTime().ToString("s") + ":" + $Message
$final | Out-File $logFile -Append
}
# Helper function to determine Copilot application and location
function Get-CopilotAppAndLocation {
param ([PSCustomObject]$AuditData)
$CopilotApp = 'Copilot for M365'
$CopilotLocation = $null
# Refine app type based on specific AppHost values first
switch ($AuditData.CopilotEventData.AppHost) {
"bizchat" { $CopilotApp = "Copilot for M365 Chat"; break }
"Outlook" { $CopilotApp = "Outlook"; break }
"Copilot Studio"{ $CopilotApp = "Copilot Studio Agent"; break }
"Word" { $CopilotApp = "Word"; break }
"Excel" { $CopilotApp = "Excel"; break }
"PowerPoint" { $CopilotApp = "PowerPoint"; break }
"Teams" { $CopilotApp = "Teams"; break }
"Bing" { $CopilotApp = "Bing"; break }
}
# Use context type as a fallback or for additional detail
if ($AuditData.copiloteventdata.contexts.type) {
switch ($AuditData.copiloteventdata.contexts.type) {
"TeamsMeeting" { $CopilotLocation = "Teams meeting" }
"StreamVideo" { $CopilotApp = "Stream"; $CopilotLocation = "Stream video player" }
}
}
if ($AuditData.AppIdentity -like "*Copilot.Studio*") {
$CopilotApp = "Copilot Studio Agent"
}
# Determine location from context ID
if ($AuditData.copiloteventdata.contexts.id -like "*/sites/*") {
$CopilotLocation = "SharePoint Online"
} elseif ($AuditData.copiloteventdata.contexts.id -like "*https://teams.microsoft.com/*") {
$CopilotLocation = if ($AuditData.copiloteventdata.contexts.id -like "*ctx=channel*") { "Teams Channel" } else { "Teams Chat" }
} elseif ($AuditData.copiloteventdata.contexts.id -like "*/personal/*") {
$CopilotLocation = "OneDrive for Business"
}
# Extract agent name from AppIdentity (legacy approach)
$AgentNameLegacy = ""
if ($CopilotApp -eq "Copilot Studio Agent" -and $AuditData.AppIdentity -match '.*[_-](.+?)$') {
$AgentNameLegacy = $Matches[1]
}
return @{ App = $CopilotApp; Location = $CopilotLocation; AgentNameLegacy = $AgentNameLegacy }
}
# Helper function to convert JSON AuditData to a flattened object
function Convert-AuditDataToObject {
param ([string]$AuditDataJson, [string]$UserID, [datetime]$CreationTime)
try {
# Helper scriptblock to sanitize strings for CSV export
$Sanitizer = {
param($InputString)
if ($null -eq $InputString) { return $null }
# Replace one or more newline/carriage return characters with a single space
$cleaned = $InputString -replace "[\r\n]+", " "
# Truncate if the string is excessively long
if ($cleaned.Length -gt 500) {
return $cleaned.Substring(0, 497) + "..."
}
return $cleaned
}
$auditDataObj = $AuditDataJson | ConvertFrom-Json
$copilotData = $auditDataObj.CopilotEventData
$appInfo = Get-CopilotAppAndLocation -AuditData $auditDataObj
$Context = $auditDataObj.copiloteventdata.contexts.id ?? $auditDataObj.copiloteventdata.threadid
# Defensively access potentially null properties
$accessedResources = $copilotData.AccessedResources ?? @()
$messages = $copilotData.Messages ?? @()
$contexts = $copilotData.Contexts ?? @()
$plugins = $copilotData.AISystemPlugin ?? @()
$models = $copilotData.ModelTransparencyDetails ?? @()
# Extract declarative agent details from the audit data
$AgentId = $auditDataObj.AgentId ?? ""
$AgentName = $auditDataObj.AgentName ?? ""
$AgentVersion = $auditDataObj.AgentVersion ?? ""
# Determine agent category based on AgentId format
$AgentCategory = ""
if ($AgentId) {
if ($AgentId -like "CopilotStudio.Declarative.*") {
$AgentCategory = "Declarative Agent"
} elseif ($AgentId -like "CopilotStudio.CustomEngine.*") {
$AgentCategory = "Custom Engine Agent"
} elseif ($AgentId -like "P_*") {
$AgentCategory = "Declarative Agent (Purview)"
} else {
$AgentCategory = "Other Agent"
}
}
# Fallback to legacy agent name extraction if new fields are empty
if ([string]::IsNullOrWhiteSpace($AgentName) -and -not [string]::IsNullOrWhiteSpace($appInfo.AgentNameLegacy)) {
$AgentName = $appInfo.AgentNameLegacy
}
# Create a custom object with flattened properties
return [PSCustomObject][Ordered]@{
TimeStamp = (Get-Date $CreationTime -format "yyyy-MM-dd HH:mm:ss")
RecordID = $auditDataObj.Id
User = $UserID
ClientRegion = $auditDataObj.ClientRegion
App = $appInfo.App
AgentName = $AgentName
AgentId = $AgentId
AgentVersion = $AgentVersion
AgentCategory = $AgentCategory
Location = $appInfo.Location
AppHost = $copilotData.AppHost
AppIdentity = $auditDataObj.AppIdentity
AppContext = $Context
ThreadId = $copilotData.ThreadId
AISystemPlugins = ($plugins | ForEach-Object { "$($_.Id):$($_.Name)" }) -join '; '
ModelDetails = ($models.ModelName) -join '; '
ContextTypes = ($contexts.Type) -join '; '
HasPrompt = @($messages.isPrompt).Contains($true)
HasResponse = @($messages.isPrompt).Contains($false)
MessageCount = $messages.Count
AccessedResourceCount = $accessedResources.Count
AccessedResourceNames = (($accessedResources.Name | Sort-Object -Unique | ForEach-Object { & $Sanitizer $_ }) -join ", ")
AccessedResourceLocations = (($accessedResources.id | Sort-Object -Unique) -join ", ")
AccessedResourceUrls = (($accessedResources.SiteUrl | Sort-Object -Unique) -join ", ")
AccessedResourceTypes = (($accessedResources.Type | Sort-Object -Unique) -join ", ")
AccessedResourceActions = (($accessedResources.action | Sort-Object -Unique | ForEach-Object { & $Sanitizer $_ }) -join ", ")
Workload = $auditDataObj.Workload
OrganizationId = $auditDataObj.OrganizationId
CopilotLogVersion = $auditDataObj.CopilotLogVersion
IsCopilotStudio = $appInfo.App -eq "Copilot Studio Agent"
IsCustomAgent = ($appInfo.App -eq "Copilot Studio Agent") -and ($AgentName -ne "")
IsSPOAgent = $copilotData.AppHost -eq "SharePoint"
IsDeclarativeAgent = ($AgentCategory -like "*Declarative*")
AuditDataJson = & $Sanitizer $AuditDataJson
}
} catch { Write-ErrorDetails -ErrorRecord $_; return $null }
}
# This variable will hold the connection status for the finally block
$isConnected = $false
# This variable will hold the final results to be returned
$functionResults = $null
try {
Write-Host "Connecting to Exchange Online..." -ForegroundColor Yellow
Connect-ExchangeOnline -ShowBanner:$false -ErrorAction Stop | Out-Null
$isConnected = $true
Write-Host "Successfully connected to Exchange Online." -ForegroundColor Green
Write-LogFile "Successfully connected to Exchange Online."
$endDate = (Get-Date).ToUniversalTime().Date.AddDays(1).AddSeconds(-1)
$startDate = (Get-Date).ToUniversalTime().Date.AddDays(-$DaysToSearch)
$allResults = [System.Collections.Generic.List[Object]]::new()
$batchSize = 5000
Write-LogFile "BEGIN: Retrieving audit records from $startDate to $endDate"
Write-Host "Searching audit logs for Copilot interactions from $startDate to $endDate" -ForegroundColor Yellow
Write-Host "Using $IntervalDays-day intervals for reliability." -ForegroundColor Yellow
$progressParams = @{ Activity = "Retrieving Copilot Audit Logs"; Status = "Initializing..." }
Write-Progress @progressParams
$totalIntervals = [math]::Ceiling(($endDate - $startDate).TotalDays / $IntervalDays)
$intervalCounter = 0
$currentStart = $startDate
while ($currentStart -le $endDate) {
$intervalCounter++
$currentEnd = ($currentStart.AddDays($IntervalDays)).AddSeconds(-1)
if ($currentEnd -gt $endDate) { $currentEnd = $endDate }
$progressParams.Status = "Interval $intervalCounter of $totalIntervals : $($currentStart.ToString('yyyy-MM-dd'))"
Write-Progress @progressParams -PercentComplete (($intervalCounter / $totalIntervals) * 50)
Write-Host "`nProcessing interval: $currentStart to $currentEnd" -ForegroundColor Cyan
$sessionID = [Guid]::NewGuid().ToString() + "_CopilotAudit"
$intervalResults = [System.Collections.Generic.List[Object]]::new()
$hasMoreRecords, $isFirstRequest = $true, $true
$pageCount, $retryCount, $maxRetries = 0, 0, 3
# ✅ FIX: Track if we've hit the 50,000 limit
$maxResultsPerSession = 50000
while ($hasMoreRecords -and $retryCount -lt $maxRetries) {
$pageCount++
Write-Host " Fetching page $pageCount..." -ForegroundColor Gray
try {
$searchParams = @{
SessionId = $sessionID
StartDate = $currentStart
EndDate = $currentEnd
ErrorAction = "Stop"
SessionCommand = "ReturnLargeSet"
}
# Only add RecordType and ResultSize on the FIRST request
if ($isFirstRequest) {
$searchParams.RecordType = "CopilotInteraction"
$searchParams.ResultSize = $batchSize
}
$batchResults = @(Search-UnifiedAuditLog @searchParams)
$isFirstRequest = $false
if ($null -ne $batchResults -and $batchResults.Count -gt 0) {
$intervalResults.AddRange($batchResults)
Write-Host " Retrieved $($batchResults.Count) records. Interval total: $($intervalResults.Count)"
# ✅ FIX: Use proper pagination logic instead of relying on ResultCount
# Stop when we receive fewer results than the batch size OR hit the 50,000 limit
if ($batchResults.Count -lt $batchSize) {
$hasMoreRecords = $false
Write-Host " Received fewer results than batch size. All available records retrieved." -ForegroundColor Green
} elseif ($intervalResults.Count -ge $maxResultsPerSession) {
$hasMoreRecords = $false
Write-Host " ⚠️ Hit the 50,000 record limit for this interval!" -ForegroundColor Yellow
Write-Host " Consider using smaller time intervals to capture all data." -ForegroundColor Yellow
}
$retryCount = 0
} else {
$hasMoreRecords = $false
Write-Host " No more records found for this interval."
}
} catch {
Write-Host " Error: $($_.Exception.Message)" -ForegroundColor Red
$retryCount++
if ($retryCount -ge $maxRetries) {
Write-Host " Max retries reached. Moving on..." -ForegroundColor Yellow
$hasMoreRecords = $false
} else {
$delay = if ($_.Exception.Message -like "*throttl*") { 60 } else { 30 }
Write-Host " Waiting $delay seconds before retrying (Attempt $retryCount of $maxRetries)..." -ForegroundColor Yellow
Start-Sleep -Seconds $delay
}
}
}
Write-Host " Interval complete: Retrieved $($intervalResults.Count) total records" -ForegroundColor $(if ($intervalResults.Count -ge $maxResultsPerSession) { 'Yellow' } else { 'Green' })
$allResults.AddRange($intervalResults)
$currentStart = $currentEnd.AddSeconds(1).Date
}
if ($allResults.Count -eq 0) {
Write-Host "No audit log entries found." -ForegroundColor Yellow
Write-LogFile "No audit log entries found."
return
}
# Deduplicate the raw results first before the expensive conversion process
Write-Host "`nDeduplicating $($allResults.Count) raw records..." -ForegroundColor Cyan
$uniqueRawResults = $allResults | Group-Object {
($_.AuditData | ConvertFrom-Json).Id
} | ForEach-Object { $_.Group[0] }
$dupCount = $allResults.Count - $uniqueRawResults.Count
if ($dupCount -gt 0) {
Write-Host "Removed $dupCount duplicate raw records before processing." -ForegroundColor Yellow
}
Write-Host "Processing $($uniqueRawResults.Count) unique records..." -ForegroundColor Cyan
$progressParams.Activity = "Processing Records"
$processedResults = [System.Collections.Generic.List[Object]]::new()
$counter = 0
foreach ($entry in $uniqueRawResults) {
$counter++
$progressParams.Status = "Record $counter of $($uniqueRawResults.Count)"
Write-Progress @progressParams -PercentComplete (50 + (($counter / $uniqueRawResults.Count) * 50))
$processedResult = Convert-AuditDataToObject -AuditDataJson $entry.AuditData -UserID $entry.UserIds -CreationTime $entry.CreationDate
if ($processedResult) { $processedResults.Add($processedResult) }
}
Write-Progress @progressParams -Completed
if ($processedResults.Count -gt 0) {
Write-Host "`nExporting $($processedResults.Count) unique records to $outputFile..." -ForegroundColor Cyan
$processedResults | Export-Csv -Path $outputFile -NoTypeInformation
Write-Host "✅ Export completed successfully!" -ForegroundColor Green
Write-LogFile "END: Exported $($processedResults.Count) records to $outputFile."
# Display summary statistics
Write-Host "`n📊 Summary Statistics:" -ForegroundColor Cyan
Write-Host " Total interactions: $($processedResults.Count)" -ForegroundColor Green
$agentStats = $processedResults | Where-Object { $_.IsDeclarativeAgent } | Measure-Object
if ($agentStats.Count -gt 0) {
Write-Host "`n📊 Declarative Agent Summary:" -ForegroundColor Cyan
Write-Host " Total declarative agent interactions: $($agentStats.Count)" -ForegroundColor Green
$uniqueAgents = $processedResults | Where-Object { $_.IsDeclarativeAgent -and $_.AgentName } | Select-Object -ExpandProperty AgentName -Unique
Write-Host " Unique agents found: $($uniqueAgents.Count)" -ForegroundColor Green
if ($uniqueAgents.Count -gt 0 -and $uniqueAgents.Count -le 10) {
Write-Host " Agent names: $($uniqueAgents -join ', ')" -ForegroundColor Gray
}
}
} else {
Write-Host "No processed records available to export." -ForegroundColor Yellow
}
# Assign the clean data to the variable that will be returned
$functionResults = $processedResults
}
catch {
Write-ErrorDetails -ErrorRecord $_
}
finally {
if ($isConnected) {
Write-Host "`nDisconnecting from Exchange Online..." -ForegroundColor Yellow
Get-PSSession | ForEach-Object { Disconnect-ExchangeOnline -PSSession $_ -Confirm:$false -ErrorAction SilentlyContinue | Out-Null }
}
}
return $functionResults
}
# Function to export Microsoft 365 Copilot usage reports
function Export-CopilotUsageReports {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$OutputFolder,
[Parameter(Mandatory = $false)]
[ValidateSet("D7", "D30", "D90", "D180")]
[string]$Period = "D30",
[Parameter(Mandatory = $false)]
[string]$LogPath
)
# Generate consistent output paths
$outputDir = if ([string]::IsNullOrWhiteSpace($OutputFolder)) {
$script:Config.DefaultOutputDirectory
} else {
$OutputFolder
}
# Create directory if it doesn't exist
if (-not (Test-Path -Path $outputDir -PathType Container)) {
New-Item -Path $outputDir -ItemType Directory -Force | Out-Null
Write-Host "Created output directory: $outputDir" -ForegroundColor Green
}
$logFilePath = if ([string]::IsNullOrWhiteSpace($LogPath)) {
Get-LogPath -BaseName "CopilotUsageReports"
} else {
$LogPath
}
# Start logging
Start-Transcript -Path $logFilePath -Append
Write-Host "Starting Microsoft 365 Copilot usage reports export. Log file: $logFilePath" -ForegroundColor Cyan
try {
# Connect to Microsoft Graph with appropriate scopes - use our fixed connect function
Write-Host "Connecting to Microsoft Graph..." -ForegroundColor Yellow
if (-not (Connect-ToMicrosoftGraph)) {
Write-Host "Cannot proceed with export. Microsoft Graph connection failed." -ForegroundColor Red
Stop-Transcript
return
}
Write-Host "Connected to Microsoft Graph successfully." -ForegroundColor Green
}
catch {
Write-Host "Error connecting to Microsoft Graph: $_" -ForegroundColor Red
Stop-Transcript
return
}
try {
# Get Copilot user usage details using direct REST API call
Write-Host "Retrieving Copilot usage details for period: $Period..." -ForegroundColor Yellow
Write-Host "This may take some time depending on the amount of data..." -ForegroundColor Yellow
$uri = "https://graph.microsoft.com/beta/reports/getMicrosoft365CopilotUsageUserDetail(period='$Period')"
Write-Host "API URI: $uri" -ForegroundColor Gray
# Initialize progress bar parameters
$progressParams = @{
Activity = "Retrieving Copilot Usage Data"
Status = "Fetching data from Microsoft Graph..."
PercentComplete = 10
}