-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathAdmin-O365AuditLog.ps1
34 lines (20 loc) · 1.17 KB
/
Admin-O365AuditLog.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
#Requires -Modules ExchangeOnlineManagement
param (
$numberDays = 1,
$outputPath = ".\output\o365Audit"
)
$currentPath = (Split-Path $MyInvocation.MyCommand.Definition -Parent)
Set-Location $currentPath
# Ensure its enabled: https://learn.microsoft.com/en-us/microsoft-365/compliance/audit-log-enable-disable?view=o365-worldwide
# Get-AdminAuditLogConfig | FL UnifiedAuditLogIngestionEnabled
Connect-ExchangeOnline
# Now you can query for Power BI activity. In this example, the results are limited to
$pivotDate = [datetime]::UtcNow.Date.AddDays(-1*$numberDays)
while ($pivotDate -le [datetime]::UtcNow) {
Write-Host "Getting audit data for: '$($pivotDate.ToString("yyyyMMdd"))'"
$results = Search-UnifiedAuditLog -StartDate $pivotDate -EndDate $pivotDate.AddHours(24).AddSeconds(-1) -RecordType PowerBIAudit -ResultSize 5000
$outputFilePath = "$outputPath\auditLogsO365\{0:yyyyMMdd}.json" -f $pivotDate
New-Item -Path (Split-Path $outputFilePath -Parent) -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
ConvertTo-Json @($results) -Compress -Depth 10 | Out-File $outputFilePath -force
$pivotDate = $pivotDate.AddDays(1)
}