-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathUpdate-Connection-Details-for-Sql-Datasource.ps1
More file actions
66 lines (52 loc) · 2.26 KB
/
Update-Connection-Details-for-Sql-Datasource.ps1
File metadata and controls
66 lines (52 loc) · 2.26 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
Write-Host
Connect-PowerBIServiceAccount | Out-Null
# update these parameters for target workspace and dataset
$workspaceName = "YOUR_WORKSPACE_NAME"
$datasetName = "YOUR_DATASET_NAME"
# add new connection details for SQL datasource
$sqlDatabaseServer = "YOUR_SERVER.database.windows.net"
$sqlDatabaseName = "YOUR_DATABASE_NAME"
# get object for target workspace
$workspace = Get-PowerBIWorkspace -Name $workspaceName
# get object for new dataset
$dataset = Get-PowerBIDataset -WorkspaceId $workspace.Id | Where-Object Name -eq $datasetName
# get object for new SQL datasource
$datasource = Get-PowerBIDatasource -WorkspaceId $workspace.Id -DatasetId $dataset.Id
# parse REST to determine gateway Id and datasource Id
$workspaceId = $workspace.Id
$datasetId = $dataset.Id
$datasourceUrl = "groups/$workspaceId/datasets/$datasetId/datasources"
# execute REST call to determine gateway Id, datasource Id and current connection details
$datasourcesResult = Invoke-PowerBIRestMethod -Method Get -Url $datasourceUrl | ConvertFrom-Json
# parse REST URL used to patch datasource credentials
$datasource = $datasourcesResult.value[0]
$gatewayId = $datasource.gatewayId
$datasourceId = $datasource.datasourceId
$sqlDatabaseServerCurrent = $datasource.connectionDetails.server
$sqlDatabaseNameCurrent = $datasource.connectionDetails.database
# parse together REST Url to update connection details
$datasourePatchUrl = "groups/$workspaceId/datasets/$datasetId/Default.UpdateDatasources"
# create HTTP request body to update datasource connection details
$postBody = @{
"updateDetails" = @(
@{
"connectionDetails" = @{
"server" = "$sqlDatabaseServer"
"database" = "$sqlDatabaseName"
}
"datasourceSelector" = @{
"datasourceType" = "Sql"
"connectionDetails" = @{
"server" = "$sqlDatabaseServerCurrent"
"database" = "$sqlDatabaseNameCurrent"
}
"gatewayId" = "$gatewayId"
"datasourceId" = "$datasourceId"
}
})
}
# convert body contents to JSON
$postBodyJson = ConvertTo-Json -InputObject $postBody -Depth 6 -Compress
# execute POST operation to update datasource connection details
Invoke-PowerBIRestMethod -Method Post -Url $datasourePatchUrl -Body $postBodyJson
# NOTE: dataset credentials must be reset after updating connection details