I am trying to upload a PBIX file to the Power BI workspace by calling an API using the powershell code below. The token returns a bearer key. I am able to retrieve the workspace ID as well, but when I try to upload the file, I am getting the below error.
Invoke-RestMethod : {"error":{"code":"UnknownError","pbi.error":{"code":"UnknownError","parameters":{},"details":[],"exceptionCulprit":1}}}
Tried "New-PowerBIReport" approach as well but got the below error.
New-PowerBIReport : Operation returned an invalid status code 'BadRequest'
Powershell code - API Approach
# Set the workspace name and API endpoint
$pbixFilePath = "C:\temp\testworkspace\test report.pbix"
$workspaceName = "testworkspace"
$apiEndpoint = "https://api.powerbi.com/v1.0/myorg"
# Connect to Power BI using a service principal
$DeploymentPrincipalAppId = "1234"
$DeploymentPrincipalAppSecret = "1234"
$tenantID = "1234"
$credentials = New-Object System.Management.Automation.PSCredential ($DeploymentPrincipalAppId, (convertto-securestring $DeploymentPrincipalAppSecret -asplaintext -force))
Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credentials -Tenant $tenantID
# Get an access token for the Power BI API
$accessToken = Get-PowerBIAccessToken
$accessToken
# Get the ID of the specified workspace
$workspace = Invoke-RestMethod -Method Get -Uri "$apiEndpoint/groups?`$filter=name eq '$workspaceName'" -Headers $accessToken
$workspaceId = $workspace.value.id
$workspaceId
# Upload the PBIX file to the workspace
$headers = @{ "Authorization" = "Bearer $accessTokenkey"; "Content-Type" = "multipart/form-data" }
$uploadUrl = "$apiEndpoint/groups/$workspaceId/imports?datasetDisplayName=$(Split-Path $pbixFilePath -Leaf)&nameConflict=Overwrite"
$uploadUrl
$response = Invoke-RestMethod -Method Post -Uri $uploadUrl -Headers $headers -InFile $pbixFilePath
New-PowerBIReport : Operation returned an invalid status code 'BadRequest'
The error usually occurs if you are not passing right parameters with New-PowerBIReport
command.
I used below PowerShell script to upload a PBIX file to the Power BI workspace like this:
$pbixFilePath = "C:\test\sri_report.pbix"
# Connect to Power BI using a service principal
$DeploymentPrincipalAppId = "1234"
$DeploymentPrincipalAppSecret = "1234"
$tenantID = "1234"
$credentials = New-Object System.Management.Automation.PSCredential ($DeploymentPrincipalAppId, (convertto-securestring $DeploymentPrincipalAppSecret -asplaintext -force))
Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credentials -Tenant $tenantID
$workspace = Get-PowerBIWorkspace -Name 'test workspace'
New-PowerBIReport -Path $pbixFilePath -Workspace $workspace -ConflictAction CreateOrOverwrite
Response:
When I checked the same in Power BI portal, PBIX file uploaded successfully to the workspace creating report like below: