-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Fetch - Catalog.ps1
297 lines (203 loc) · 11.9 KB
/
Fetch - Catalog.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
#Requires -Modules MicrosoftPowerBIMgmt.Profile
param(
[psobject]$config
,
[bool]$reset = $false
,
[string]$stateFilePath
)
try {
Write-Host "Starting Power BI Catalog Fetch"
$stopwatch = [System.Diagnostics.Stopwatch]::new()
$stopwatch.Start()
$outputPath = "$($config.OutputPath)\catalog"
if (!$stateFilePath) {
$stateFilePath = "$($config.OutputPath)\state.json"
}
if (Test-Path $stateFilePath) {
$state = Get-Content $stateFilePath | ConvertFrom-Json
#ensure mandatory fields
$state | Add-Member -NotePropertyName "Catalog" -NotePropertyValue (new-object PSObject) -ErrorAction SilentlyContinue
$state.Catalog | Add-Member -NotePropertyName "LastRun" -NotePropertyValue $null -ErrorAction SilentlyContinue
$state.Catalog | Add-Member -NotePropertyName "LastFullScan" -NotePropertyValue $null -ErrorAction SilentlyContinue
}
else {
$state = New-Object psobject
$state | Add-Member -NotePropertyName "Catalog" -NotePropertyValue @{"LastRun" = $null; "LastFullScan" = $null } -Force
$state.Catalog.LastRun = [datetime]::UtcNow.Date.ToString("o")
}
# ensure folders
$scansOutputPath = Join-Path $outputPath ("scans\{0:yyyy}\{0:MM}\{0:dd}" -f [datetime]::Today)
$snapshotOutputPath = Join-Path $outputPath ("snapshots\{0:yyyy}\{0:MM}\{0:dd}" -f [datetime]::Today)
New-Item -ItemType Directory -Path $scansOutputPath -ErrorAction SilentlyContinue | Out-Null
New-Item -ItemType Directory -Path $snapshotOutputPath -ErrorAction SilentlyContinue | Out-Null
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)"
#region ADMIN API
$snapshotFiles = @()
$filePath = "$snapshotOutputPath\apps.json"
$snapshotFiles += $filePath
if (!(Test-Path $filePath)) {
Write-Host "Getting Power BI Apps List"
$result = Invoke-PowerBIRestMethod -Url "admin/apps?`$top=5000&`$skip=0 " -Method Get | ConvertFrom-Json
$result = @($result.value)
if ($result.Count -ne 0) {
ConvertTo-Json $result -Depth 10 -Compress | Out-File $filePath -force
}
else {
Write-Host "Tenant without PowerBI apps"
}
}
else {
Write-Host "'$filePath' already exists"
}
# Save to Blob
if ($config.StorageAccountConnStr) {
Write-Host "Writing Snapshots to Blob Storage"
$storageRootPath = "$($config.StorageAccountContainerRootPath)/catalog"
foreach ($outputFilePath in $snapshotFiles) {
if (Test-Path $outputFilePath) {
Add-FileToBlobStorage -storageAccountConnStr $config.StorageAccountConnStr -storageContainerName $config.StorageAccountContainerName -storageRootPath $storageRootPath -filePath $outputFilePath -rootFolderPath $outputPath
Remove-Item $outputFilePath -Force
}
else {
Write-Warning "Cannot find file '$outputFilePath'"
}
}
}
#endregion
#region Workspace Scans: 1 - Get Modified; 2 - Start Scan for modified; 3 - Wait for scan finish; 4 - Get Results
Write-Host "Getting workspaces to scan"
$getInfoDetails = "lineage=true&datasourceDetails=true&getArtifactUsers=true&datasetSchema=false&datasetExpressions=false"
if ($config.CatalogGetInfoParameters) {
$getInfoDetails = $config.CatalogGetInfoParameters
}
$getModifiedWorkspacesParams = "excludePersonalWorkspaces=false&excludeInActiveWorkspaces=true"
if ($config.CatalogGetModifiedParameters) {
$getModifiedWorkspacesParams = $config.CatalogGetModifiedParameters
}
$fullScan = $false
if ($state.Catalog.LastRun -and !$reset) {
if (!($state.Catalog.LastRun -is [datetime])) {
$state.Catalog.LastRun = [datetime]::Parse($state.Catalog.LastRun).ToUniversalTime()
}
if ($config.FullScanAfterDays) {
if ($state.Catalog.LastFullScan) {
if (!($state.Catalog.LastFullScan -is [datetime])) {
$state.Catalog.LastFullScan = [datetime]::Parse($state.Catalog.LastFullScan).ToUniversalTime()
}
$daysSinceLastFullScan = ($state.Catalog.LastRun - $state.Catalog.LastFullScan).TotalDays
if ($daysSinceLastFullScan -ge $config.FullScanAfterDays) {
Write-Host "Triggering FullScan after $daysSinceLastFullScan days"
$fullScan = $true
}
else {
Write-Host "Days to next fullscan: $($config.FullScanAfterDays - $daysSinceLastFullScan)"
}
}
else {
Write-Host "Triggering FullScan, because FullScanAfterDays is configured and LastFullScan is empty"
$fullScan = $true
}
}
if (!$fullScan) {
$modifiedSinceDate = $state.Catalog.LastRun
$modifiedSinceDateMinDate = [datetime]::UtcNow.Date.AddDays(-30)
if ($modifiedSinceDate -le $modifiedSinceDateMinDate) {
Write-Host "Last Run date was '$($modifiedSinceDate.ToString("o"))' but cannot go longer than 30 days"
$modifiedSinceDate = $modifiedSinceDateMinDate
}
$getModifiedWorkspacesParams = $getModifiedWorkspacesParams + "&modifiedSince=$($modifiedSinceDate.ToString("o"))"
}
}
else {
$fullScan = $true
}
$modifiedRequestUrl = "admin/workspaces/modified?$getModifiedWorkspacesParams"
Write-Host "Reset: $reset"
Write-Host "Since: $($state.Catalog.LastRun)"
Write-Host "FullScan: $fullScan"
Write-Host "Last FullScan: $($state.Catalog.LastFullScan)"
Write-Host "FullScanAfterDays: $($config.FullScanAfterDays)"
Write-Host "GetModified parameters '$getModifiedWorkspacesParams'"
Write-Host "GetInfo parameters '$getInfoDetails'"
# Get Modified Workspaces since last scan (Max 30 per hour)
$workspacesModified = Invoke-PowerBIRestMethod -Url $modifiedRequestUrl -Method Get | ConvertFrom-Json
if (!$workspacesModified -or $workspacesModified.Count -eq 0) {
Write-Host "No workspaces modified"
}
else {
Write-Host "Modified workspaces: $($workspacesModified.Count)"
$throttleErrorSleepSeconds = 3700
$scanStatusSleepSeconds = 5
$getInfoOuterBatchCount = 1500
$getInfoInnerBatchCount = 100
Write-Host "Throttle Handling Variables: getInfoOuterBatchCount: $getInfoOuterBatchCount; getInfoInnerBatchCount: $getInfoInnerBatchCount; throttleErrorSleepSeconds: $throttleErrorSleepSeconds"
# postworkspaceinfo only allows 16 parallel requests, Get-ArrayInBatches allows to create a two level batch strategy. It should support initial load without throttling on tenants with ~50000 workspaces
Get-ArrayInBatches -array $workspacesModified -label "GetInfo Global Batch" -batchCount $getInfoOuterBatchCount -script {
param($workspacesModifiedOuterBatch, $i)
$script:workspacesScanRequests = @()
# Call GetInfo in batches of 100 (MAX 500 requests per hour)
Get-ArrayInBatches -array $workspacesModifiedOuterBatch -label "GetInfo Local Batch" -batchCount $getInfoInnerBatchCount -script {
param($workspacesBatch, $x)
Wait-On429Error -tentatives 1 -sleepSeconds $throttleErrorSleepSeconds -script {
$bodyStr = @{"workspaces" = @($workspacesBatch.Id) } | ConvertTo-Json
# $script: scope to reference the outerscope variable
$getInfoResult = @(Invoke-PowerBIRestMethod -Url "admin/workspaces/getInfo?$getInfoDetails" -Body $bodyStr -method Post | ConvertFrom-Json)
$script:workspacesScanRequests += $getInfoResult
}
}
# Wait for Scan to execute - https://docs.microsoft.com/en-us/rest/api/power-bi/admin/workspaceinfo_getscanstatus (10,000 requests per hour)
while (@($workspacesScanRequests | Where-Object status -in @("Running", "NotStarted"))) {
Write-Host "Waiting for scan results, sleeping for $scanStatusSleepSeconds seconds..."
Start-Sleep -Seconds $scanStatusSleepSeconds
foreach ($workspaceScanRequest in $workspacesScanRequests) {
$scanStatus = Invoke-PowerBIRestMethod -Url "admin/workspaces/scanStatus/$($workspaceScanRequest.id)" -method Get | ConvertFrom-Json
Write-Host "Scan '$($scanStatus.id)' : '$($scanStatus.status)'"
$workspaceScanRequest.status = $scanStatus.status
}
}
# Get Scan results (500 requests per hour) - https://docs.microsoft.com/en-us/rest/api/power-bi/admin/workspaceinfo_getscanresult
foreach ($workspaceScanRequest in $workspacesScanRequests) {
Wait-On429Error -tentatives 1 -sleepSeconds $throttleErrorSleepSeconds -script {
$scanResult = Invoke-PowerBIRestMethod -Url "admin/workspaces/scanResult/$($workspaceScanRequest.id)" -method Get | ConvertFrom-Json
Write-Host "Scan Result'$($scanStatus.id)' : '$($scanResult.workspaces.Count)'"
$fullScanSuffix = ""
if ($fullScan) {
$fullScanSuffix = ".fullscan"
}
$outputFilePath = "$scansOutputPath\$($workspaceScanRequest.id)$fullScanSuffix.json"
$scanResult | Add-Member –MemberType NoteProperty –Name "scanCreatedDateTime" –Value $workspaceScanRequest.createdDateTime -Force
ConvertTo-Json $scanResult -Depth 10 -Compress | Out-File $outputFilePath -force
# Save to Blob
if ($config.StorageAccountConnStr -and (Test-Path $outputFilePath)) {
Write-Host "Writing to Blob Storage"
$storageRootPath = "$($config.StorageAccountContainerRootPath)/catalog"
Add-FileToBlobStorage -storageAccountConnStr $config.StorageAccountConnStr -storageContainerName $config.StorageAccountContainerName -storageRootPath $storageRootPath -filePath $outputFilePath -rootFolderPath $outputPath
Remove-Item $outputFilePath -Force
}
}
}
}
}
#endregion
# Save State
Write-Host "Saving state"
New-Item -Path (Split-Path $stateFilePath -Parent) -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
$state.Catalog.LastRun = [datetime]::UtcNow.Date.ToString("o")
if ($fullScan) {
$state.Catalog.LastFullScan = [datetime]::UtcNow.Date.ToString("o")
}
ConvertTo-Json $state | Out-File $stateFilePath -force -Encoding utf8
}
finally {
$stopwatch.Stop()
Write-Host "Elapsed: $($stopwatch.Elapsed.TotalSeconds)s"
}