Repository navigation
Expand file tree
/
Copy pathScan_PBIB.ps1
More file actions
311 lines (244 loc) · 8.82 KB
/
Copy pathScan_PBIB.ps1
File metadata and controls
311 lines (244 loc) · 8.82 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
Param(
[Parameter(Mandatory = $true)]
[string]$DefinitionPath,
[switch]$DebugMode,
[switch]$ShowTables,
[switch]$ShowColumns,
[switch]$ShowMeasures,
[string]$FilterPrefix
)
# -----------------------------
# FIXED DEFAULT SELECTION LOGIC
# -----------------------------
# Case 1: user chose NOTHING → show EVERYTHING
if (-not $ShowTables -and -not $ShowColumns -and -not $ShowMeasures) {
$ShowTables = $true
$ShowColumns = $true
$ShowMeasures = $true
}
# Case 2: user chose ONLY ShowTables → table-only mode
if ($ShowTables -and -not $ShowColumns -and -not $ShowMeasures) {
$Mode = "TablesOnly"
} else {
$Mode = "Normal"
}
$prefixUsed = $FilterPrefix -and $FilterPrefix.Trim() -ne ""
function DBG { if ($DebugMode) { param($m) Write-Host $m -ForegroundColor DarkGray } }
Write-Host "Scanning PBIR folder:"
Write-Host $DefinitionPath
# -----------------------------
# INTERNAL STORAGE
# -----------------------------
$results = New-Object System.Collections.ArrayList
$visualTables = @{}
function Add-Table {
param($Page, $Visual, $Table)
$key = "$Page|$Visual"
if (-not $visualTables.ContainsKey($key)) {
$visualTables[$key] = New-Object System.Collections.Generic.HashSet[string]
}
$visualTables[$key].Add($Table) | Out-Null
}
function Add-Entry {
param($Page, $Visual, $Table, $Name, $Type)
# Always track table → used by ShowTables mode
if ($Table) { Add-Table $Page $Visual $Table }
# Table-only mode: ignore columns/measures entirely
if ($Mode -eq "TablesOnly") { return }
# Apply type filters
if ($Type -eq "Column" -and -not $ShowColumns) { return }
if ($Type -eq "Measure" -and -not $ShowMeasures) { return }
# Prefix filtering
if ($prefixUsed) {
$p = $FilterPrefix.ToLower()
if (-not ($Table.ToLower().StartsWith($p) -or $Name.ToLower().StartsWith($p))) {
return
}
}
$null = $results.Add([PSCustomObject]@{
PageGuid = $Page
VisualGuid = $Visual
Table = $Table
Name = $Name
Type = $Type
})
}
# -----------------------------
# FIELD EXTRACTION
# -----------------------------
function ScanField {
param($field, $Page, $Visual)
# Column
if ($field.PSObject.Properties.Name -contains "Column") {
$c = $field.Column
Add-Entry $Page $Visual $c.Expression.SourceRef.Entity $c.Property "Column"
}
# Measure
if ($field.PSObject.Properties.Name -contains "Measure") {
$m = $field.Measure
Add-Entry $Page $Visual $m.Expression.SourceRef.Entity $m.Property "Measure"
}
# Sparkline
if ($field.PSObject.Properties.Name -contains "SparklineData") {
$s = $field.SparklineData
if ($s.Measure.Measure.Expression.SourceRef.Entity) {
Add-Entry $Page $Visual $s.Measure.Measure.Expression.SourceRef.Entity $s.Measure.Measure.Property "Measure"
}
if ($s.Groupings) {
foreach ($g in $s.Groupings) {
if ($g.PSObject.Properties.Name -contains "Column") {
$gc = $g.Column
Add-Entry $Page $Visual $gc.Expression.SourceRef.Entity $gc.Property "Column"
}
if ($g.PSObject.Properties.Name -contains "HierarchyLevel") {
$h = $g.HierarchyLevel
Add-Entry $Page $Visual $h.Expression.Hierarchy.Expression.SourceRef.Entity $h.Level "Column"
}
}
}
}
}
# -----------------------------
# JSON RECURSION
# -----------------------------
function ScanNode {
param($node, $Page, $Visual)
if ($null -eq $node) { return }
if ($node -is [System.Management.Automation.PSCustomObject]) {
$keys = $node.PSObject.Properties.Name
if ($keys -contains "field") {
ScanField $node.field $Page $Visual
}
if ($keys -contains "sortDefinition") {
foreach ($s in $node.sortDefinition.sort) {
Add-Entry $Page $Visual $s.field.Column.Expression.SourceRef.Entity $s.field.Column.Property "Column"
}
}
foreach ($prop in $node.PSObject.Properties) {
ScanNode $prop.Value $Page $Visual
}
return
}
if ($node -is [System.Collections.IDictionary]) {
foreach ($key in $node.Keys) {
ScanNode $node[$key] $Page $Visual
}
return
}
if ($node -is [System.Collections.IEnumerable] -and -not ($node -is [string])) {
foreach ($x in $node) {
ScanNode $x $Page $Visual
}
return
}
}
# -----------------------------
# MAIN SCAN
# -----------------------------
$pagesPath = Join-Path $DefinitionPath "pages"
foreach ($page in (Get-ChildItem $pagesPath -Directory)) {
$pageId = $page.Name
$visualsPath = Join-Path $page.FullName "visuals"
if (-not (Test-Path $visualsPath)) { continue }
foreach ($visual in (Get-ChildItem $visualsPath -Directory)) {
$visualId = $visual.Name
$visualJson = Join-Path $visual.FullName "visual.json"
if (-not (Test-Path $visualJson)) { continue }
$json = Get-Content $visualJson -Raw | ConvertFrom-Json
ScanNode $json $pageId $visualId
}
}
# -----------------------------
# OUTPUT SECTION
# -----------------------------
Write-Host ""
Write-Host "=== RESULTS ===" -ForegroundColor Yellow
Write-Host ""
# ============================================================
# ✅ TABLE-ONLY MODE (CORRECTED)
# ============================================================
if ($Mode -eq "TablesOnly") {
$tablesPrinted = @{}
foreach ($visualKey in $visualTables.Keys) {
$tbls = @()
foreach ($t in $visualTables[$visualKey]) {
if ($prefixUsed) {
if ($t.ToLower().StartsWith($FilterPrefix.ToLower())) {
$tbls += $t
}
} else {
$tbls += $t
}
}
if ($prefixUsed -and $tbls.Count -eq 0) { continue }
if (-not $prefixUsed) {
foreach ($t in $tbls) {
if (-not $tablesPrinted.ContainsKey($t)) {
Write-Host "Table: $t" -ForegroundColor Green
$tablesPrinted[$t] = $true
}
}
continue
}
# prefix → show page + visual
$parts = $visualKey.Split("|")
Write-Host "Page: $($parts[0])" -ForegroundColor Cyan
Write-Host " Visual: $($parts[1])" -ForegroundColor Magenta
foreach ($t in $tbls) {
Write-Host " Table: $t" -ForegroundColor Green
}
Write-Host ""
}
exit
}
# ============================================================
# ✅ NORMAL MODE (Columns + Measures)
# ============================================================
$pg = $results | Group-Object PageGuid
foreach ($page in $pg) {
$pageId = $page.Name
$visuals = $page.Group | Group-Object VisualGuid
if ($prefixUsed) {
$pageHas = $false
foreach ($vis in $visuals) {
$filtered = $vis.Group | Where-Object {
$_.Table.ToLower().StartsWith($FilterPrefix.ToLower()) -or
$_.Name.ToLower().StartsWith($FilterPrefix.ToLower())
}
if ($filtered.Count -gt 0) { $pageHas = $true }
}
if (-not $pageHas) { continue }
}
if ($prefixUsed) {
Write-Host "Page: $pageId" -ForegroundColor Cyan
}
foreach ($vis in $visuals) {
$visualId = $vis.Name
$entries = $vis.Group
if ($prefixUsed) {
$entries = $entries | Where-Object {
$_.Table.ToLower().StartsWith($FilterPrefix.ToLower()) -or
$_.Name.ToLower().StartsWith($FilterPrefix.ToLower())
}
}
if ($entries.Count -eq 0) { continue }
if ($prefixUsed) {
Write-Host " Visual: $visualId" -ForegroundColor Magenta
}
foreach ($tbl in ($entries | Group-Object Table)) {
if ($prefixUsed) {
Write-Host " Table: $($tbl.Name)" -ForegroundColor Green
} else {
Write-Host "Table: $($tbl.Name)" -ForegroundColor Green
}
foreach ($row in $tbl.Group) {
if ($prefixUsed) {
Write-Host " - $($row.Type): $($row.Name)"
} else {
Write-Host "- $($row.Type): $($row.Name)"
}
}
}
if ($prefixUsed) { Write-Host "" }
}
}