【发布时间】:2021-09-22 02:52:26
【问题描述】:
我正在使用如下服务原理连接到 Power Bi。
$PbiSecurePassword = ConvertTo-SecureString $ClientSecret -Force -AsPlainText
$PbiCredential = New-Object Management.Automation.PSCredential($ClientId, $PbiSecurePassword)
Connect-PowerBIServiceAccount -ServicePrincipal -TenantId $TenantId -Credential $PbiCredential
$headers = Get-PowerBIAccessToken
然后我尝试更新数据集值,如下所示
$datasourePostUrl = "https://api.powerbi.com/v1.0/myorg/groups/$workspaceId/datasets/$datasetId/Default.UpdateDatasources"
# create HTTP request body to update datasource connection details
$postBody = @{
"updateDetails" = @(
@{
"connectionDetails" = @{
"server" = "$dbserver"
"database" = "$dbname"
}
"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
#Added UseDefaultCredentials & UserAgent based on the stack overflow answer https://stackoverflow.com/a/16735376/6862041 & https://stackoverflow.com/a/27882588/6862041
$userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome
# execute POST operation to update datasource connection details
Invoke-RestMethod -Headers $headers -Method Post -Uri $datasourePostUrl -UserAgent $userAgent -UseDefaultCredentials -Body $postBodyJson -ContentType $ContentType
我收到以下错误
Exception : System.Net.WebException: The remote server returned an error: (403) Forbidden.
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
ErrorCategory : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
不知道我错过了什么。任何帮助将不胜感激。
【问题讨论】:
-
403 错误表明用户凭据正确,但您连接的用户没有更新数据源连接的访问权限。
-
我在 Azure Ad 中创建了此服务原则,并授予了此 Power Bi 工作区的权限。当我尝试将其嵌入我的网络应用程序时,这是有效的。但是当我尝试构建具有相同服务原则的 CI/CD 时,它不起作用。
标签: powershell azure-devops powerbi