0

I am connecting to the Power Bi using the service principle like below.

$PbiSecurePassword = ConvertTo-SecureString $ClientSecret -Force -AsPlainText
$PbiCredential = New-Object Management.Automation.PSCredential($ClientId, $PbiSecurePassword)
Connect-PowerBIServiceAccount -ServicePrincipal -TenantId $TenantId -Credential $PbiCredential
$headers = Get-PowerBIAccessToken

Then I am trying to update the dataset values like below

$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 

I am getting the following error

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

Not sure what I am missing. Any help would be much appreciated.

Joseph Abraham
  • 347
  • 4
  • 10
  • 1
    403 error would suggest that user credentials are correct, but the user that you're connecting as doesn't have access rights to update data source connection. – qbik Jul 13 '21 at 03:51
  • I have created this service principle in Azure Ad and given the permissions to this Power Bi workspace. This is working when I am trying to embed it into my web app. But it's not working when I am trying to build a CI/CD with the same Service principle. – Joseph Abraham Jul 13 '21 at 04:01

1 Answers1

0

If the service principal is having necessary perms at Workspace/dataset.

There could be a setting (Allow Service Principals to use Power BI APIs. ) at the tenant level which could be causing the error - if the setting is in the disabled state - you re likely to encounter this behavior.

Note : This is in disabled state by Default.

You will have to access the below as an Power BI service Admin

Power BI Admin Portal --> Tenant Settings --> Developer Settings --> Allow Service Principals to use Power BI APIs.

enter image description here

Toggle it to Enabled state.

You can either enable this feature to the entire org or to specific set of Service Principal belonging to a Security Group.

Click 'Apply'.

Satya V
  • 3,811
  • 1
  • 6
  • 9