-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Fetch - Activity.ps1
163 lines (119 loc) · 5.53 KB
/
Fetch - Activity.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
#Requires -Modules MicrosoftPowerBIMgmt.Profile
param(
[psobject]$config
,
[string]$stateFilePath
)
try {
Write-Host "Starting Power BI Activity Fetch"
$stopwatch = [System.Diagnostics.Stopwatch]::new()
$stopwatch.Start()
if ($config.ActivityFileBatchSize)
{
$outputBatchCount = $config.ActivityFileBatchSize
}
else {
$outputBatchCount = 5000
}
$rootOutputPath = "$($config.OutputPath)\activity"
New-Item -ItemType Directory -Path $rootOutputPath -ErrorAction SilentlyContinue | Out-Null
$outputPath = "$rootOutputPath\{0:yyyy}\{0:MM}"
if (!$stateFilePath) {
$stateFilePath = "$($config.OutputPath)\state.json"
}
if (Test-Path $stateFilePath) {
$state = Get-Content $stateFilePath | ConvertFrom-Json
}
else {
$state = New-Object psobject
}
$maxHistoryDate = [datetime]::UtcNow.Date.AddDays(-30)
if ($state.Activity.LastRun) {
if (!($state.Activity.LastRun -is [datetime])) {
$state.Activity.LastRun = [datetime]::Parse($state.Activity.LastRun).ToUniversalTime()
}
$pivotDate = $state.Activity.LastRun
}
else {
$state | Add-Member -NotePropertyName "Activity" -NotePropertyValue @{"LastRun" = $null } -Force
$pivotDate = $maxHistoryDate
}
if ($pivotDate -lt $maxHistoryDate)
{
Write-Host "Last run was more than 30 days ago"
$pivotDate = $maxHistoryDate
}
Write-Host "Since: $($pivotDate.ToString("s"))"
Write-Host "OutputBatchCount: $outputBatchCount"
Write-Host "Getting OAuth Token"
if ($config.ServicePrincipal.AppId)
{
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $config.ServicePrincipal.AppId, ($config.ServicePrincipal.AppSecret | ConvertTo-SecureString -AsPlainText -Force)
$pbiAccount = Connect-PowerBIServiceAccount -ServicePrincipal -Tenant $config.ServicePrincipal.TenantId -Credential $credential -Environment $config.ServicePrincipal.Environment
}
else {
$pbiAccount = Connect-PowerBIServiceAccount
}
Write-Host "Login with: $($pbiAccount.UserName)"
# Gets audit data for each day
while ($pivotDate -le [datetime]::UtcNow) {
Write-Host "Getting audit data for: '$($pivotDate.ToString("yyyyMMdd"))'"
$activityAPIUrl = "admin/activityevents?startDateTime='$($pivotDate.ToString("s"))'&endDateTime='$($pivotDate.AddHours(24).AddSeconds(-1).ToString("s"))'"
$audits = @()
$pageIndex = 1
$flagNoActivity = $true
do
{
if (!$result.continuationUri)
{
$result = Invoke-PowerBIRestMethod -Url $activityAPIUrl -method Get | ConvertFrom-Json
}
else {
$result = Invoke-PowerBIRestMethod -Url $result.continuationUri -method Get | ConvertFrom-Json
}
if ($result.activityEventEntities)
{
$audits += @($result.activityEventEntities)
}
if ($audits.Count -ne 0 -and ($audits.Count -ge $outputBatchCount -or $null -eq $result.continuationToken))
{
# To avoid duplicate data on existing files, first dont append pageindex to overwrite existing full file
if ($pageIndex -eq 1)
{
$outputFilePath = ("$outputPath\{0:yyyyMMdd}.json" -f $pivotDate)
}
else {
$outputFilePath = ("$outputPath\{0:yyyyMMdd}_$pageIndex.json" -f $pivotDate)
}
Write-Host "Writing '$($audits.Count)' audits"
New-Item -Path (Split-Path $outputFilePath -Parent) -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
ConvertTo-Json @($audits) -Compress -Depth 10 | Out-File $outputFilePath -force
if ($config.StorageAccountConnStr -and (Test-Path $outputFilePath)) {
Write-Host "Writing to Blob Storage"
$storageRootPath = "$($config.StorageAccountContainerRootPath)/activity"
Add-FileToBlobStorage -storageAccountConnStr $config.StorageAccountConnStr -storageContainerName $config.StorageAccountContainerName -storageRootPath $storageRootPath -filePath $outputFilePath -rootFolderPath $rootOutputPath
Write-Host "Deleting local file '$outputFilePath'"
Remove-Item $outputFilePath -Force
}
$flagNoActivity = $false
$pageIndex++
$audits = @()
}
}
while($null -ne $result.continuationToken)
if ($flagNoActivity)
{
Write-Warning "No audit logs for date: '$($pivotDate.ToString("yyyyMMdd"))'"
}
$state.Activity.LastRun = $pivotDate.Date.ToString("o")
$pivotDate = $pivotDate.AddDays(1)
# Save state
Write-Host "Saving state"
New-Item -Path (Split-Path $stateFilePath -Parent) -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
ConvertTo-Json $state | Out-File $stateFilePath -force -Encoding utf8
}
}
finally {
$stopwatch.Stop()
Write-Host "Elapsed: $($stopwatch.Elapsed.TotalSeconds)s"
}