-
Notifications
You must be signed in to change notification settings - Fork 0
/
Export-FabricItemsFromAllWorkspaces.ps1
226 lines (183 loc) · 9.81 KB
/
Export-FabricItemsFromAllWorkspaces.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
<#
.SYNOPSIS
Exports all items from all active Workspaces in the Fabric/Power BI tenant to a local folder.
.DESCRIPTION
This script exports all items from all active Workspaces in the Fabric/Power BI tenant to a local folder.
.PARAMETER ConfigObject
A PSCustomObject containing the configuration settings for the script. The default value is the contents of the Config.json file in the same directory as the script.
The object should have the following structure:
@{
ServicePrincipal = @{
TenantId = 'YOUR_TENANT_ID'
AppId = 'YOUR_APP_ID'
AppSecret = 'YOUR_APP_SECRET'
}
}
.PARAMETER IgnoreObject
A PSCustomObject containing the names of Workspaces and Reports to ignore. The default value is the contents of the IgnoreList.json file in the same directory as the script.
The object should have the following structure:
@{
IgnoreWorkspaces = @('Workspace1', 'Workspace2')
IgnoreReports = @('Report1', 'Report2')
}
.PARAMETER WorkspaceFilter
The filter expression for which Workspaces to export. Default value is '(type eq ''Workspace'') and (state eq ''Active'')'.
.PARAMETER ModuleUrl
The URL of the FabricPS-PBIP.psm1 module. Default value is 'https://raw.githubusercontent.com/microsoft/Analysis-Services/master/pbidevmode/fabricps-pbip/FabricPS-PBIP.psm1'.
.PARAMETER RetentionCutoffDate
The cutoff date for retention of exported items. Default value is 12:00AM on the current date minus 30 days.
The datatype is [datetime], so the input must be expressed as either:
- A datetime-formatted string (e.g. '2024-01-01', '2024-01-01T00:00:00', etc.)
- A [datetime] object (e.g. (Get-Date).Date.AddDays(-30), (Get-Date).Date.AddYears(-1), etc.)
.PARAMETER TargetFolder
The path to the folder where the items will be exported. If not provided, the items will be exported to a folder named 'Workspaces\YYYY\MM\DD' in the same directory as the script.
.PARAMETER GetLatestModule
If specified, the script will download the latest version of the FabricPS-PBIP.psm1 module from the Analysis-Services repository.
.PARAMETER ConvertToTmdl
If specified, the script will convert model.bim files into 'definition' TMDL folder using pbi-tools.
.INPUTS
None - Pipeline input is not accepted.
.OUTPUTS
None - Pipeline output is not produced.
.LINK
[Source code](https://github.com/JamesDBartlett3/Fabric-Archive-Bot)
.LINK
[Follow the author's blog](https://datavolume.xyz)
.LINK
[Follow the author on GitHub](https://github.com/JamesDBartlett3)
.LINK
[Follow the author on LinkedIn](https://www.linkedin.com/in/jamesdbartlett3/)
.LINK
[Follow the author on Mastodon](https://techhub.social/@JamesDBartlett3)
.LINK
[Follow the author on BlueSky](https://bsky.app/profile/jamesdbartlett3.bsky.social)
#>
Param(
[Parameter()][PSCustomObject]$ConfigObject = (Get-Content -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Config.json') | ConvertFrom-Json),
[Parameter()][PSCustomObject]$IgnoreObject = (Get-Content -Path (Join-Path -Path $PSScriptRoot -ChildPath 'IgnoreList.json') | ConvertFrom-Json),
[Parameter()][string]$WorkspaceFilter = '(type eq ''Workspace'') and (state eq ''Active'')',
[Parameter()][string]$ModuleUrl = 'https://raw.githubusercontent.com/microsoft/Analysis-Services/master/pbidevmode/fabricps-pbip/FabricPS-PBIP.psm1',
[Parameter()][datetime]$RetentionCutoffDate = (Get-Date).Date.AddDays(-30),
[Parameter()][string]$TargetFolder = (Join-Path -Path $PSScriptRoot -ChildPath 'Workspaces'),
[Parameter()][switch]$GetLatestModule,
[Parameter()][switch]$ConvertToTmdl
)
# If NuGet package provider is not installed, install it
if (-not ((Get-PackageProvider).Name -contains 'NuGet')) {
Register-PackageSource -Name 'NuGet.org' -Location 'https://api.nuget.org/v3/index.json' -ProviderName 'NuGet'
}
# If Az.Account module is not installed, install it
if (-not (Get-Module -Name Az.Accounts -ListAvailable)) {
Install-Module -Name Az.Accounts -Scope CurrentUser
}
# Declare $moduleName variable
[string]$moduleFileName = Split-Path -Leaf $ModuleUrl
# Declare $localModulePath variable
[string]$localModulePath = (Join-Path -Path $PSScriptRoot -ChildPath $moduleFileName)
# Download latest FabricPS-PBIP.psm1 from Analysis-Services repository if it does not exist, or if $GetLatestModule is specified
if (-not (Test-Path -Path $localModulePath) -or ($GetLatestModule)) {
Remove-Module FabricPS-PBIP -ErrorAction SilentlyContinue
Remove-Item $localModulePath -ErrorAction SilentlyContinue
Invoke-WebRequest -Uri $ModuleUrl -OutFile $localModulePath
}
# Unblock the downloaded FabricPS-PBIP.psm1 file so it can be imported
Unblock-File -Path $localModulePath
# Import the FabricPS-PBIP module
Import-Module $localModulePath -ErrorAction SilentlyContinue
# Get names of Workspaces and Reports to ignore from the $IgnoreObject parameter
[array]$ignoreWorkspaces = $IgnoreObject.IgnoreWorkspaces
# TODO: Implement IgnoreReports
# [array]$ignoreReports = $IgnoreObject.IgnoreReports
# Get configuration settings from the $ConfigObject parameter
[string]$tenantId = $ConfigObject.ServicePrincipal.TenantId
[string]$servicePrincipalId = $ConfigObject.ServicePrincipal.AppId
[string]$servicePrincipalSecret = $ConfigObject.ServicePrincipal.AppSecret
# Instantiate $useServicePrincipal variable as $true if Service Principal credentials are provided in the $ConfigObject parameter
[bool]$useServicePrincipal = $tenantId -and $servicePrincipalId -and $servicePrincipalSecret
# Get current date and create a folder hierarchy for the year, month, and day
[datetime]$date = Get-Date
[string]$year = $date.Year.ToString()
[string]$month = $date.Month.ToString("D2")
[string]$day = $date.Day.ToString("D2")
# Declare $sep variable to use as platform-agnostic directory separator
[string]$sep = [IO.Path]::DirectorySeparatorChar
# Add the year, month, and day to the target folder path
$TargetFolder = Join-Path -Path $TargetFolder -ChildPath ($year + $sep + $month + $sep + $day)
# Create the target folder if it does not exist
if (-not (Test-Path -Path $TargetFolder)) {
New-Item -Path $TargetFolder -ItemType Directory -Force | Out-Null
}
# Initialize the $loopCount variable
[int]$loopCount = 0
# Define the function to get the headers for the Fabric REST API
# If $useServicePrincipal is $true, use the Service Principal credentials to get the headers
# Otherwise, use the current user's credentials
Function Get-FabricHeaders {
if ($useServicePrincipal) {
if ($loopCount -eq 0) {
Logout-AzAccount | Out-Null
}
Set-FabricAuthToken -TenantId $tenantId -servicePrincipalId $servicePrincipalId -servicePrincipalSecret $servicePrincipalSecret
} else {
Set-FabricAuthToken
}
return @{
Authorization = "Bearer $(Get-FabricAuthToken)"
}
}
$headers = Get-FabricHeaders
# Get a list of all active Workspaces in batches of 5000 until all workspaces have been fetched
# TODO: Replace Power BI API call with Fabric API call: https://learn.microsoft.com/en-us/rest/api/fabric/admin/workspaces/list-workspaces?tabs=HTTP
[guid[]]$workspaceIds = @()
[int]$skip = 0
[int]$batchSize = 5000
do {
[string]$batchUri = 'https://api.powerbi.com/v1.0/myorg/admin/groups?$filter={0}&$top={1}&$skip={2}' -f $WorkspaceFilter, $batchSize, $skip
$batch = Invoke-RestMethod -Uri $batchUri -Method GET -Headers $headers
$workspaceIds += $batch.value | Where-Object {
$_.name -notin $ignoreWorkspaces
} | Select-Object -ExpandProperty id
$skip += $batchSize
} while ($batch.value.Count -eq $batchSize)
# Export contents of each Workspace to the target folder
$workspaceIds | ForEach-Object {
[guid]$workspaceId = $_
# Export all items from the Workspace to the target folder
Export-FabricItems -WorkspaceId $workspaceId -Path $TargetFolder -ErrorAction SilentlyContinue
# If $ConvertToTmdl is specified, convert the model.bim file to a .tmdl folder with Microsoft.AnalysisServices.Tabular
if ($ConvertToTmdl) {
$bimFiles = Get-ChildItem -Path (Join-Path -Path $TargetFolder -ChildPath $workspaceId) -Filter '*.bim' -Recurse -File
foreach ($bimFile in $bimFiles) {
$tmdlFolder = Join-Path -Path $bimFile.DirectoryName -ChildPath 'definition'
$modelText = Get-Content $bimFile.FullName
$database = [Microsoft.AnalysisServices.Tabular.JsonSerializer]::DeserializeDatabase($modelText, $null, [Microsoft.AnalysisServices.CompatibilityMode]::PowerBI)
[Microsoft.AnalysisServices.Tabular.TmdlSerializer]::SerializeDatabaseToFolder($database, $tmdlFolder)
}
}
$headers = Get-FabricHeaders
# Get the name of the Workspace and rename the folder to the Workspace name
[string]$workspaceName = (Invoke-RestMethod -Uri "https://api.fabric.microsoft.com/v1/admin/workspaces/$workspaceId" -Method GET -Headers $headers).name
Remove-Item -Recurse (Join-Path -Path $TargetFolder -ChildPath $workspaceName) -Force -ErrorAction SilentlyContinue
Rename-Item -Path (Join-Path -Path $TargetFolder -ChildPath $workspaceId) -NewName $workspaceName -Force -ErrorAction SilentlyContinue
$loopCount += 1
}
# Measure the hierarchy depth of a folder
Function Measure-FolderDepth($path) {
$absolutePath = Resolve-Path $path
$parts = Split-Path $absolutePath -NoQualifier
$folderDepth = $parts.Split($sep).Count
return $folderDepth
}
# Measure the depth of the target folder
[int]$targetFolderDepth = Measure-FolderDepth $TargetFolder
# Get list of all archives older than $RetentionCutoffDate
$oldFolders = Get-ChildItem -Path $TargetFolder -Directory -Recurse -Depth 2 | Where-Object { $_.CreationTime -lt $RetentionCutoffDate }
# Remove old folders deeper than the target folder depth + 2
foreach($oldFolder in $oldFolders) {
[int]$folderDepth = Measure-FolderDepth $oldFolder
if ($folderDepth -gt $targetFolderDepth + 2) {
Remove-Item -Path $oldFolder -Recurse -Force -ErrorAction SilentlyContinue
}
}
# Remove empty folders
Get-ChildItem -Path $TargetFolder -Directory -Recurse | Where-Object { $_.GetFiles().Count -eq 0 -and $_.GetDirectories().Count -eq 0 } | Remove-Item -Force -ErrorAction SilentlyContinue