-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy-pbixreport-with-gateway.ps1
227 lines (177 loc) · 7.3 KB
/
deploy-pbixreport-with-gateway.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
write-host "clientId: $env:clientId"
write-host "clientsecret: $env:clientsecret"
write-host "tenantId: $env:tenantId"
write-host "workspacename: $env:workspacename"
write-host "userAdminEmail: $env:userAdmin"
write-host "pbixFilePath: $env:pbixFilePath"
write-host "dbServerParamName: $env:dbServerParamName"
write-host "dbServerParamValue: $env:dbServerParamValue"
write-host "dbNameParamName: $env:dbNameParamName"
write-host "dbNameParamValue: $env:dbNameParamValue"
write-host "reportName: $env:reportName"
## ------------------------------------------------------
## 1. SIGN IN WITH SP
## ------------------------------------------------------
write-host "`n...sign in with SP"
$clientsec = "$env:clientsecret" | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $env:clientId, $clientsec
Connect-PowerBIServiceAccount `
-ServicePrincipal `
-Credential $credential `
-TenantId $env:tenantId
## ------------------------------------------------------
## 2. MANAGE WORKSPACE
## ------------------------------------------------------
write-host "`n...Manage Workspace"
$workspace = Get-PowerBIWorkspace `
-Name $env:workspacename
# create the workspace if it does not exists
if($null -eq $workspace){
write-host "- creating new $env:workspacename workspace"
New-PowerBIWorkspace `
-Name $env:workspacename
$workspace = Get-PowerBIWorkspace `
-Name $env:workspacename
} else {
write-host "- $env:workspacename workspace already exists"
}
## ------------------------------------------------------
# 3. ADD AN ADMIN USER TO THE WORKSPACE
## ------------------------------------------------------
write-host "`n...Adding admin user to the workspace"
# check if the admin user already exists
$wsusersResponse = Invoke-PowerBIRestMethod `
-Url "groups/$($workspace.id)/users" `
-Method GET `
| ConvertFrom-Json
$wsusers = $wsusersResponse.value | where-object {$_.emailAddress -eq $env:userAdmin}
if ($null -eq $wsusers) {
Write-Host "- Adding user '$env:userAdmin' to '$env:workspacename' workspace."
Add-PowerBIWorkspaceUser `
-Id $($workspace.id) `
-UserPrincipalName $env:userAdmin `
-AccessRight "Admin"
} else {
write-host "- User $env:userAdmin' already has access to the '$env:workspacename' workspace."
}
## ------------------------------------------------------
## 4. UPLOAD PBIX REPORT
## ------------------------------------------------------
write-host "`n...Importing .pbix report."
$new_report = New-PowerBIReport `
-Path $env:pbixFilePath `
-Name $env:reportName `
-ConflictAction "CreateOrOverwrite" `
-Workspace $workspace
write-Host ".pbix report uploaded";
# Get the report again because the dataset id is not immediately available with New-PowerBIReport
$new_report = Get-PowerBIReport `
-WorkspaceId "$($workspace.id)" `
-Id $new_report.id
## ------------------------------------------------------
## 5. UPDATE DB PARAMS
## ------------------------------------------------------
write-host "`n...Take over the dataset with the SP"
# get the embedded dataset of the new report
$dataset = Get-PowerBIDataset `
-WorkspaceId "$($workspace.id)" `
-Id $new_report.datasetId
# take over
Invoke-PowerBIRestMethod `
-Url "groups/$($workspace.id)/datasets/$($dataset.id)/Default.TakeOver" `
-Method Post `
-ErrorAction Stop;
write-host "Take over with SP complete"
#Change the database server and db / params
write-host "`n...Update its DB params"
$dbParams = @{
updateDetails = @(
[pscustomobject]@{name=$env:dbServerParamName;newValue=$env:dbServerParamValue}
[pscustomobject]@{name=$env:dbNameParamName;newValue=$env:dbNameParamValue}
)
} | ConvertTo-Json
Invoke-PowerBIRestMethod `
-Url "groups/$($workspace.id)/datasets/$($dataset.id)/Default.UpdateParameters" `
-Method POST `
-Body $dbParams `
-ErrorAction Stop;
write-host "DB param change complete";
## ------------------------------------------------------
## 6. BIND GATEWAY
## ------------------------------------------------------
# 6.1 Sign in with user account
write-host "`n...Sign in using user account"
$userAdminPassword = $env:userAdminPassword | ConvertTo-SecureString -asPlainText -Force
$uacreds= New-Object System.Management.Automation.PSCredential($env:userAdmin, $userAdminPassword)
Connect-PowerBIServiceAccount `
-Credential $uacreds
# 6.2 take over dataset with user account
write-host "`n...Take over dataset with user account"
Invoke-PowerBIRestMethod `
-Url "groups/$($workspace.id)/datasets/$($dataset.id)/Default.TakeOver" `
-Method Post `
-ErrorAction Stop;
# 6.3 Discover bindable gateways
write-host "`n...Discover Gateways"
$gatewayDataSources = Invoke-PowerBIRestMethod `
-Url "groups/$($workspace.id)/datasets/$($dataset.id)/Default.DiscoverGateways" `
-Method GET | ConvertFrom-Json `
-ErrorAction Stop;
# 6.3 get only the target gateway
$gateway = $gatewayDataSources.value | Where-Object {$_.name -eq $env:gatewayName}
write-host "Found Gateway '$env:gatewayName'; gatewayObjectId=$($gateway.id)"
# 6.4 Bind
write-host "`n...Binding to Gateway"
$bindReqBody = @{
gatewayObjectId = "$($gateway.id)"
} | ConvertTo-Json
Invoke-PowerBIRestMethod `
-Url "groups/$($workspace.id)/datasets/$($dataset.id)/Default.BindToGateway" `
-Method Post `
-Body $bindReqBody `
-ErrorAction Stop;
write-Host "Gateway binding completed";
## ------------------------------------------------------
## 7. SET THE REFRESH SCHEDULE (If provided)
## ------------------------------------------------------
if($null -ne $env:scheduleJson)
{
write-Host "`n...Creating Refresh Schedule...";
Invoke-PowerBIRestMethod `
-Method PATCH `
-Url "groups/$($workspace.id)/datasets/$($dataset.id)/refreshSchedule" `
-Body $env:scheduleJson `
-ErrorAction Stop;
write-Host "Saved Refresh Schedule";
}
## ------------------------------------------------------
## 8. REFRESH THE DATASET
## ------------------------------------------------------
# Note!!! only a max of 8 refreshes allowed in the non-premium workspace, after which 400/Bad Requests will be recieved
# Note2 - Refresh must be issued after the bind to gateway otherwise the gateway's datasource will not be mapped.
write-host "`n...Refreshing dataset"
# Note: using NoNotification for sample, but if using MailOnFailure, MailOnCompletion will need to setup emails via the api otherwise will recieved 400/Bad Request
$refreshBody = @{
notifyOption ="NoNotification"
} | ConvertTo-Json
Invoke-PowerBIRestMethod `
-Url "groups/$($workspace.id)/datasets/$($dataset.id)/refreshes" `
-Method POST `
-Body $refreshBody `
-ErrorAction Stop;
write-Host "Refresh completed" -ForegroundColor Green;
## ------------------------------------------------------
## 9. ASSIGN DS BACK TO SP
## ------------------------------------------------------
# wait for 5 secs
Start-Sleep -s 5
write-host "`n...Take back ownership with the SP account"
$clientsec = "$env:clientSecret" | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $env:clientId, $clientsec
Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credential -TenantId $env:tenantId
# take over
Invoke-PowerBIRestMethod `
-Url "groups/$($workspace.id)/datasets/$($dataset.id)/Default.TakeOver" `
-Method Post `
-ErrorAction Stop;
write-Host "Take over to SP completed"