【问题标题】:How to update Azure SQL datasource credentials of PowerBI dataset programmatically?如何以编程方式更新 Power BI 数据集的 Azure SQL 数据源凭据?
【发布时间】:2021-07-22 08:59:12
【问题描述】:

我有一个 Azure Devops 进程,它使用服务原则从 .pbix 文件部署 Power BI 报告,并更新数据源以指向生产 Azure SQL 服务器。

结果是部署了报表和数据集并更新了连接,但是 CREDENTIALS 为空白(用户名和密码),因此为了使其可用,必须有人登录到 Power BI 服务,打开数据集和更新凭据,这意味着我们的 CI/CD 流程涉及手动步骤。

我需要有关通过代码更新新数据源的源凭据的帮助,以便不需要此手动过程

任何建议都会有很大帮助。谢谢。

【问题讨论】:

  • 您好 Rohith,请问您是否要在 Azure DevOps 管道完成后更新源凭据?还是您想在管道结束时使用 PS 脚本更新凭据?

标签: azure-devops powerbi powerbi-datasource


【解决方案1】:

这是一个示例 PowerShell 脚本,使用 Microsoft Power BI Cmdlets,它将 patch the credentials of the dataset 并在末尾注释了几行,之后将 refresh the dataset(如果需要,取消注释)。只需将顶部的 x-es 替换为实际值(工作区和数据集名称、应用程序 ID 等)即可。

<#

Patch the credentials of a published report/dataset, so it can be refreshed.

#>

# Fill these ###################################################
$tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # Get from Azure AD -> Properties (https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/Properties)
$applictionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # Get Application (client) ID from Azure AD -> App registrations (https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps)
$applicationSecret = "xxxxxxxxxxxxxxxx" # Create it from application's "Certificates & secrets" section
$workspaceName = "xxxxxxxx"
$reportName = "xxxxxxxx" # Actually it is dataset name
$sqlUserName = "xxxxxxxx"
$sqlUserPassword = "xxxxxxxxxx"
################################################################

Import-Module MicrosoftPowerBIMgmt

$SecuredApplicationSecret = ConvertTo-SecureString -String $applicationSecret -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($applictionId, $SecuredApplicationSecret)

$sp = Connect-PowerBIServiceAccount -ServicePrincipal -Tenant $tenantId -Credential $credential

$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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-30
    • 2021-11-21
    • 1970-01-01
    • 2018-10-05
    • 2022-08-09
    • 2023-03-12
    • 1970-01-01
    • 2021-08-21
    相关资源
    最近更新 更多