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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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