-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathHack - FixReportConnections.ps1
147 lines (99 loc) · 4.88 KB
/
Hack - FixReportConnections.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
#Requires -Modules @{ ModuleName="MicrosoftPowerBIMgmt"; ModuleVersion="1.2.1026" } -Assembly System.IO.Compression
<#
WARNING - This script will change the internal files of your PBIX files. A backup will be made but its not supported by Microsoft.
#>
$currentPath = (Split-Path $MyInvocation.MyCommand.Definition -Parent)
$reportsPath = "C:\Users\ruiromano\OneDrive - Microsoft\Work\202110\MMeyersSamples\Reports\Sales*"
$datasetId = "80cc1541-7ba7-4a52-b7f2-496f58389975"
$workingDir = "$currentPath\_temp\fixconnections"
$backupdir = "$currentPath\_temp\fixconnections\bkup"
$sharedDatasetsPath = "$currentPath\shareddatasets.json"
if (!(Test-Path $sharedDatasetsPath))
{
Write-Warning "Cannot find shareddatasets file '$sharedDatasetsPath'. Login to app.powerbi.com and execute a networktrace and save the 'sharedatasets' request to file: '$sharedDatasetsPath'."
return
}
# Ensure folders exists
@($workingDir, $backupDir) |% { New-Item -ItemType Directory -Path $_ -Force -ErrorAction SilentlyContinue | Out-Null }
$reports = Get-ChildItem -File -Path "$reportsPath" -Include "*.pbix" -Recurse -ErrorAction SilentlyContinue
if ($reports.Count -eq 0)
{
Write-Host "No reports on path '$reportsPath'"
return
}
# Connect to PBI
Connect-PowerBIServiceAccount
$sharedDataSetsStr = Get-Content $sharedDatasetsPath
#ConverFrom-Json doesnt like properties with same name
$sharedDataSetsStr = $sharedDataSetsStr.Replace("nextRefreshTime","nextRefreshTime_").Replace("lastRefreshTime","lastRefreshTime_")
$sharedDataSets = $sharedDataSetsStr | ConvertFrom-Json
$reports |% {
$pbixFile = $_
Write-Host "Fixing connection of report: '$($pbixFile.Name)'"
$filePath = $pbixFile.FullName
$fileName = [System.IO.Path]::GetFileName($pbixFile.FullName)
$fileNameWithoutExt = [System.IO.Path]::GetFileNameWithoutExtension($fileName)
Write-Host "Finding dataset model id of dataset '$dataSetId'"
# Find model for the dataset
$model = $sharedDataSets |? { $_.model.dbName -eq $dataSetId }
if (!$model)
{
Write-Host "Cannot find a Power BI model for dataset '$dataSetId'"
}
else
{
$modelId = $model.modelId
Write-Host "Found Power BI model '$modelId' for '$dataSetId'"
Write-Host "Backup '$fileName' into '$backupDir'"
Copy-Item -Path $filePath -Destination "$backupDir\$fileNameWithoutExt.$(Get-Date -Format "yyyyMMddHHmmss").pbix" -Force
$zipFile = "$workingDir\$fileName.zip"
$zipFolder = "$workingDir\$fileNameWithoutExt"
Write-Host "Unziping '$fileName' into $zipFolder"
Copy-Item -Path $filePath -Destination $zipFile -Force
Expand-Archive -Path $zipFile -DestinationPath $zipFolder -Force | Out-Null
$connectionsJson = Get-Content "$zipFolder\Connections" | ConvertFrom-Json
$connection = $connectionsJson.Connections[0]
$remoteArtifactId = $null
if($connectionsJson.RemoteArtifacts -and $connectionsJson.RemoteArtifacts.Count -ne 0)
{
$remoteArtifactId = $connectionsJson.RemoteArtifacts[0].DatasetId
}
if ($connection.PbiModelDatabaseName -eq $dataSetId -and $remoteArtifactId -eq $dataSetId)
{
Write-Warning "PBIX '$fileName' already connects to dataset '$dataSetId' skipping the rebind"
return
}
$connection.PbiServiceModelId = $modelId
$connection.ConnectionString = $connection.ConnectionString.Replace($connection.PbiModelDatabaseName, $dataSetId)
$connection.PbiModelDatabaseName = $dataSetId
if ($remoteArtifactId)
{
$connectionsJson.RemoteArtifacts[0].DatasetId = $dataSetId
}
$connectionsJson | ConvertTo-Json -Compress | Out-File "$zipFolder\Connections" -Encoding ASCII
# Update the connections on zip file
Write-Host "Updating connections file on zip file"
Compress-Archive -Path "$zipFolder\Connections" -CompressionLevel Optimal -DestinationPath $zipFile -Update
# Remove SecurityBindings
Write-Host "Removing SecurityBindings"
try{
$stream = new-object IO.FileStream($zipfile, [IO.FileMode]::Open)
$zipArchive = new-object IO.Compression.ZipArchive($stream, [IO.Compression.ZipArchiveMode]::Update)
$securityBindingsFile = $zipArchive.Entries |? Name -eq "SecurityBindings" | Select -First 1
if ($securityBindingsFile)
{
$securityBindingsFile.Delete()
}
else
{
Write-Host "Cannot find SecurityBindings on zip"
}
}
finally{
if ($zipArchive) { $zipArchive.Dispose() }
if ($stream) { $stream.Dispose() }
}
Write-Host "Overwriting original pbix"
Copy-Item -Path $zipfile -Destination $filePath -Force
}
}