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
Seadog
Regular Visitor

Export Report REST API returns corrupted file

Hello,

 

First of all, I am sorry if I posted this in a wrong topic section, but I could not find any better to fit the Power BI REST API. I have a problem with the "Export Report In Group" functionality. I am using PowerShell to execute the call and save the file. My code works, I do not get any errors. I get a valid response and the files are saved. Problem is, that I cannot open the file with Power BI Desktop, failing with the following error:

 

2018-07-24 08_19_43-.png

I save the files in "pbix" format. I tried saving them as "zip" and no format whatsoever but none of the combination works as expected. The reports were created and published using Power BI May 2018 and I tried to open them with the same version and Power BI June 2018. Can anyone help me with this problem?

 

 

My PowerShell code looks as following (I dotted out the Ids):

 

# declare parameters

$sourceReportGroupId = "..."
$destinationFolder = "C:\Users\pawel.kucharski\Documents\MTV\Azure\Downloaded"
$clientId = "..." 

# End Parameters =======================================

# Calls the Active Directory Authentication Library (ADAL) to authenticate against AAD
function GetAuthToken
{
    $adal = "${env:ProgramFiles}\WindowsPowerShell\Modules\AzureRM.profile\5.3.3\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
    
    $adalforms = "${env:ProgramFiles}\WindowsPowerShell\Modules\AzureRM.profile\5.3.3\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"
 
    [System.Reflection.Assembly]::LoadFrom($adal) | Out-Null

    [System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null

    $redirectUri = "https://oauth.powerBI.com/PBIMTVAdmin"

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

    $authority = "https://login.microsoftonline.com/common/oauth2/authorize";

    $authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority

    $authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")

    return $authResult
}

# Get the auth token from AAD
$token = GetAuthToken

# Building Rest API header with authorization token
$authHeader = @{
   #'Content-Type'='application/zip'
   'Authorization'=$token.CreateAuthorizationHeader()
}

# properly format groups path
$sourceGroupsPath = ""
if ($sourceReportGroupId -eq "me") {
    $sourceGroupsPath = "myorg"
} else {
    $sourceGroupsPath = "myorg/groups/$sourceReportGroupId"
}


# Get reports
$uri = "https://api.powerbi.com/v1.0/$sourceGroupsPath/reports"
$reports = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET -Verbose

#$reports | ConvertTo-Json | Out-File "C:\Users\kuchapaw\Documents\PowerBI\responseJson.json"

#$reports.value | Out-File "C:\Users\kuchapaw\Documents\PowerBI\response.json"


#export each report from the group

foreach($report in $reports.value)
{
    $reportId = $report.id
    $reportName = $report.name

    $filePath = "$destinationFolder/$reportName.pbix"

    $uri2 = "https://api.powerbi.com/v1.0/$sourceGroupsPath/reports/$reportId/Export"
    $reportResponse = Invoke-RestMethod -Uri $uri2 -Headers $authHeader -Method GET -Verbose 
    $reportResponse | Out-File $filePath
}

Thank you!

1 ACCEPTED SOLUTION
v-jiascu-msft
Employee
Employee

Hi @Seadog,

 

Try to change the last two lines like below. It works in my test.

$reportResponse = Invoke-RestMethod -Uri $uri2 -Headers $authHeader -Method GET -OutFile c.pbix -Verbose 
# $reportResponse | Out-File a.pbix
}

Best Regards,

Dale

Community Support Team _ Dale
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

3 REPLIES 3
v-jiascu-msft
Employee
Employee

Hi @Seadog,

 

Try to change the last two lines like below. It works in my test.

$reportResponse = Invoke-RestMethod -Uri $uri2 -Headers $authHeader -Method GET -OutFile c.pbix -Verbose 
# $reportResponse | Out-File a.pbix
}

Best Regards,

Dale

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

Thank you, that is what caused the problem. I have found this solution as well in PowerBIPS, which I can recommend to anyone who plans to use the Power BI API extensively.

Seadog
Regular Visitor

I changed the encoding to ANSI but the result is still the same

 

$reportResponse | Out-File $filePath -Encoding ascii

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.