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.