Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
GarethNW
New Member

Problem publishing a .pbix file to PBI Workspace using the rest api and PowerShell

I am trying to publish a Power BI Report via a PowerShell script. Has anyone had much experience with this as I can't quite get it to work using this method.

 

 

$clientId = "****"
$clientSecret = "****"
$tenantId = "****"
$apiUrl = "https://api.powerbi.com/v1.0/myorg/groups/{Workspace}/imports?datasetDisplayName={DatadetName}"

$pbixFilePath = ".\BIDeploymentPlan.pbix"

$resourceUrl = "https://analysis.windows.net/powerbi/api"

$tokenBody = @{
    grant_type    = "client_credentials"
    client_id     = $clientId
    client_secret = $clientSecret
    resource      = $resourceUrl
    scope         = "https://graph.microsoft.com/.default"
}

$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantId/oauth2/token" `
    -Method POST -Body $tokenBody

$accessToken = $tokenResponse.access_token

$headers = @{
    "Authorization" = "Bearer $accessToken"
}

$body = @{
    filePath = $pbixFilePath
} | ConvertTo-Json -Depth 1

$restMethodParams = @{
    Uri         = $apiUrl
    Method      = 'Post'
    Headers     = $headers
    Body        = $body
    ContentType = 'application/json'
}

Invoke-RestMethod @restMethodParams

 

 

I have used the following document to build this script: https://learn.microsoft.com/en-us/rest/api/power-bi/

Thank you for any help you can offer.

No issue with my access token as the following code works fine: Invoke-RestMethod -Uri $apiUrl -Method Delete -Headers $headers

1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @GarethNW ,

 

I suggest you to refer to below code to publish your pbix file to Power BI Service by PowerShell.

 

$clientID = "<ClientID>"

$filePath = "C:\Users\...\MyPBIXTest.pbix" 

$groupname = "WorkspaceName"

$datasetDisplayName = "MyImportTest" 

$nameConflict = "Overwrite"

$preferClientRouting = "false"

 

#Get Token

$authToken = Get-PBIAuthToken -ClientId $clientID -Credential (Get-Credential)

 

#Get Group ID

$group = Get-PBIGroup -name $groupname

 

#Set URI for Import call

$powerBIUri = "https://api.powerbi.com/v1.0/myorg/"

$uri = "groups/$($group.id)/imports?datasetDisplayName=$datasetDisplayName&preferClientRouting=$preferClientRouting"

 

# Initializing

$body = ""

$contentType = $null

 

#Setting boundary for string

$boundary = [System.Guid]::NewGuid().ToString("N")

$LF = [System.Environment]::NewLine

 

#Getting content using the file path

$fileName = [uri]::EscapeDataString([IO.Path]::GetFileName($filePath))

$fileBin = [IO.File]::ReadAllBytes($filePath)

$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")

$fileEnc = $enc.GetString($fileBin)

 

#Set body

$body = (

"--$boundary",

"Content-Disposition: form-data; name=`"file0`"; filename=`"$fileName`"; filename*=UTF-8''$fileName",

"Content-Type: application/x-zip-compressed$LF",

$fileEnc,

"--$boundary--$LF"

) -join $LF

 

#Set content type

$contentType = "multipart/form-data; boundary=--$boundary"

 

#Set API URL

$restApiUrl = $powerBIUri + $uri

 

#Set Header

$authHeader = @</span>{

'Content-Type'='application/json'

'Authorization'="Bearer $authToken"}

 

 

# Invoke REST Method

$response = Invoke-RestMethod -Uri $restApiUrl -Headers $authHeader -Body $body -Method "POST" -contentType $contentType -Verbose

 

Make sure your PowerShell is installed correctly.

For reference: Power BI Cmdlets reference | Microsoft Learn

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

View solution in original post

1 REPLY 1
v-rzhou-msft
Community Support
Community Support

Hi @GarethNW ,

 

I suggest you to refer to below code to publish your pbix file to Power BI Service by PowerShell.

 

$clientID = "<ClientID>"

$filePath = "C:\Users\...\MyPBIXTest.pbix" 

$groupname = "WorkspaceName"

$datasetDisplayName = "MyImportTest" 

$nameConflict = "Overwrite"

$preferClientRouting = "false"

 

#Get Token

$authToken = Get-PBIAuthToken -ClientId $clientID -Credential (Get-Credential)

 

#Get Group ID

$group = Get-PBIGroup -name $groupname

 

#Set URI for Import call

$powerBIUri = "https://api.powerbi.com/v1.0/myorg/"

$uri = "groups/$($group.id)/imports?datasetDisplayName=$datasetDisplayName&preferClientRouting=$preferClientRouting"

 

# Initializing

$body = ""

$contentType = $null

 

#Setting boundary for string

$boundary = [System.Guid]::NewGuid().ToString("N")

$LF = [System.Environment]::NewLine

 

#Getting content using the file path

$fileName = [uri]::EscapeDataString([IO.Path]::GetFileName($filePath))

$fileBin = [IO.File]::ReadAllBytes($filePath)

$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")

$fileEnc = $enc.GetString($fileBin)

 

#Set body

$body = (

"--$boundary",

"Content-Disposition: form-data; name=`"file0`"; filename=`"$fileName`"; filename*=UTF-8''$fileName",

"Content-Type: application/x-zip-compressed$LF",

$fileEnc,

"--$boundary--$LF"

) -join $LF

 

#Set content type

$contentType = "multipart/form-data; boundary=--$boundary"

 

#Set API URL

$restApiUrl = $powerBIUri + $uri

 

#Set Header

$authHeader = @</span>{

'Content-Type'='application/json'

'Authorization'="Bearer $authToken"}

 

 

# Invoke REST Method

$response = Invoke-RestMethod -Uri $restApiUrl -Headers $authHeader -Body $body -Method "POST" -contentType $contentType -Verbose

 

Make sure your PowerShell is installed correctly.

For reference: Power BI Cmdlets reference | Microsoft Learn

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Helpful resources

Announcements
March PBI video - carousel

Power BI Monthly Update - March 2025

Check out the March 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors