下午好,
我们的开发人员使用 PBI 创建了一些不错的报告,但问题是他们使用数据库 X。这些数据库仅包含开发数据。一旦他们想要发布它,我们必须调整他们的报告以使用包含生产数据的数据库 Y。
开发人员有没有办法发布他们的报告,将数据库从 X 更新到 Y,但不添加凭据? 通过这种方式,他们的报告将通过网关刷新,网关将设置为使用 UID/PWD 链接到 DB Y(用户无法访问)
谢谢:)
答案 0 :(得分:1)
好吧,某人毕竟知道数据库 Y 的密码,对吧?为什么不在 Power BI Desktop 中打开报表、更改数据源并发布它?但是,当数据源仍然是开发数据库(数据库 X)时,可以选择重定向开发人员发布的报告。
首先,他们可以使用 connection specific parameters 来定义数据源。然后,在发布后,知道凭据的人更新参数值以将数据库重定向到生产数据库并输入凭据。
第二个不需要对报告进行任何更改。让您的开发人员发布它,然后使用 Power BI REST API 到 change the datasource 和 patch the credentials。例如,这可以通过使用 Power BI Management CmdLets 的 PowerShell 来完成。首先,以管理员身份启动 PowerShell 并通过执行以下脚本安装 CmdLets:
Install-Module MicrosoftPowerBIMgmt
如果您在计算机上没有管理员权限,或者由于某些其他原因不想为所有用户安装这些,您可以只为当前用户安装:
Install-Module MicrosoftPowerBIMgmt -Scope CurrentUser
你没有提到使用的是什么数据库。这很重要,因此在下面的示例中,我将假定它是 SQL Server。更新前几行中的值以指向您的生产数据库和已发布的报告,然后执行脚本以更改数据源:
# Fill these ###################################################
$workspaceName = "The name of the workspace where the report is published"
$datasetName = "The name of the report"
$sqlDatabaseServer = "Server name"
$sqlDatabaseName = "Database name"
$username = "powerbiuser@yourcompany.com"
$password = "the password of the power bi user" | ConvertTo-SecureString -asPlainText -Force
################################################################
Import-Module MicrosoftPowerBIMgmt
Clear-Host
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
Connect-PowerBIServiceAccount -Credential $credential | Out-Null
$workspace = Get-PowerBIWorkspace -Name $workspaceName
$dataset = Get-PowerBIDataset -WorkspaceId $workspace.Id -Name $datasetName
$datasource = Get-PowerBIDatasource -WorkspaceId $workspace.Id -DatasetId $dataset.Id
# Construct url
$workspaceId = $workspace.Id
$datasetId = $dataset.Id
$datasourceUrl = "groups/$workspaceId/datasets/$datasetId/datasources"
# Call the REST API to get gateway Id, datasource Id and current connection details
$datasourcesResult = Invoke-PowerBIRestMethod -Method Get -Url $datasourceUrl | ConvertFrom-Json
# Parse the response
$datasource = $datasourcesResult.value[0]
$gatewayId = $datasource.gatewayId
$datasourceId = $datasource.datasourceId
$sqlDatabaseServerCurrent = $datasource.connectionDetails.server
$sqlDatabaseNameCurrent = $datasource.connectionDetails.database
# Construct url for update
$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"
}
})
}
$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
现在更新以下脚本开头的值并执行它以修补生产数据库的凭据:
# Fill these ###################################################
$workspaceName = "The name of the workspace where the report is published"
$reportName = "The name of the report"
$sqlUserName = "user name"
$sqlUserPassword = "password"
$username = "powerbiuser@yourcompany.com"
$password = "the password of the power bi user" | ConvertTo-SecureString -asPlainText -Force
################################################################
Import-Module MicrosoftPowerBIMgmt
Clear-Host
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
Connect-PowerBIServiceAccount -Credential $credential | Out-Null
$workspace = Get-PowerBIWorkspace -Name $workspaceName
$dataset = Get-PowerBIDataset -WorkspaceId $workspace.Id -Name $reportName
$workspaceId = $workspace.Id
$datasetId = $dataset.Id
$datasources = Get-PowerBIDatasource -WorkspaceId $workspaceId -DatasetId $datasetId
foreach($datasource in $datasources) {
$gatewayId = $datasource.gatewayId
$datasourceId = $datasource.datasourceId
$datasourePatchUrl = "gateways/$gatewayId/datasources/$datasourceId"
Write-Host "Patching credentials for $datasourceId"
# HTTP request body to patch datasource credentials
$userNameJson = "{""name"":""username"",""value"":""$sqlUserName""}"
$passwordJson = "{""name"":""password"",""value"":""$sqlUserPassword""}"
$patchBody = @{
"credentialDetails" = @{
"credentials" = "{""credentialData"":[ $userNameJson, $passwordJson ]}"
"credentialType" = "Basic"
"encryptedConnection" = "NotEncrypted"
"encryptionAlgorithm" = "None"
"privacyLevel" = "Organizational"
}
}
# Convert body contents to JSON
$patchBodyJson = ConvertTo-Json -InputObject $patchBody -Depth 6 -Compress
# Execute PATCH operation to set datasource credentials
Invoke-PowerBIRestMethod -Method Patch -Url $datasourePatchUrl -Body $patchBodyJson
}
$datasetRefreshUrl = "groups/$workspaceId/datasets/$datasetId/refreshes"
Write-Host "Refreshing..."
Invoke-PowerBIRestMethod -Method Post -Url $datasetRefreshUrl