Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
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
Solved! Go to Solution.
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.
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.
Check out the September 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
38 | |
4 | |
4 | |
3 | |
3 |