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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Anonymous
Not applicable

'Post in group' with powershell

I need to import some files to Power BI using API and powershell

 

First I create a temporary upload with: groups/group_id/imports/createTemporaryUploadLocation

 

Then I push my file to the URL

Invoke-RestMethod -Method PUT -Uri \"${url}\" -Header @{ \'x-ms-blob-type\' = \'BlockBlob\'; \'Content-Length\' = $len } -InFile '$myFile' -TimeoutSec 3600

 

Finally, I try to import that URL to PowerBi using:

Invoke-WebRequest -Method POST -Uri "https://api.powerbi.com/v1.0/myorg/groups/group_id/imports?datasetDisplayName=$myFile&nameConflict=O..." -Header @{Authorization = '$Token'; 'Content-Length' = $len} -Body '{ "fileUrl"= "url"}' | ConvertTo-Json -Depth 5

 

I am getting the following error: 

Invoke-WebRequest : You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.

 What am I doing wrong? 

 

EDIT:

After some more research, I found the bellow powershell code to import to PowerBI. It is working but it won't let me import larger file 900,000KB

I also still need to find out how to get the files from azure...

 

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$localPath = ''
$graphToken = ''

$groupId = ''

$importMode="CreateOrOverwrite"
if(!$ReportName){
$ReportName = (Get-Item -LiteralPath $localPath).BaseName
}
if($groupId){
$uri = "https://api.powerbi.com/v1.0/myorg/groups/$groupId/imports?datasetDisplayName=$ReportName&nameConfli..."
}else{
$uri = "https://api.powerbi.com/v1.0/myorg/imports?datasetDisplayName=$ReportName&nameConflict=$importMode"
}
$boundary = "---------------------------" + (Get-Date).Ticks.ToString("x")
$boundarybytes = [System.Text.Encoding]::ASCII.GetBytes("`r`n--" + $boundary + "`r`n")
$request = [System.Net.WebRequest]::Create($uri)
$request.ContentType = "multipart/form-data; boundary=" + $boundary
$request.Method = "POST"
$request.KeepAlive = $true
$request.Headers.Add("Authorization", "Bearer $graphToken")
$rs = $request.GetRequestStream()
$rs.Write($boundarybytes, 0, $boundarybytes.Length);
$header = "Content-Disposition: form-data; filename=`"temp.pbix`"`r`nContent-Type: application / octet - stream`r`n`r`n"
$headerbytes = [System.Text.Encoding]::UTF8.GetBytes($header)
$rs.Write($headerbytes, 0, $headerbytes.Length);
$fileContent = [System.IO.File]::ReadAllBytes($localPath)
$rs.Write($fileContent,0,$fileContent.Length)
$trailer = [System.Text.Encoding]::ASCII.GetBytes("`r`n--" + $boundary + "--`r`n");
$rs.Write($trailer, 0, $trailer.Length);
$rs.Flush()
$rs.Close()
Write-Host $request
$response = $request.GetResponse()
$stream = $response.GetResponseStream()
$streamReader = [System.IO.StreamReader]($stream)
$content = $streamReader.ReadToEnd() | convertfrom-json
$jobId = $content.id
$streamReader.Close()
$response.Close()
$header = @{
'Authorization' = 'Bearer ' + $graphToken}

Write-Host $("Deployment request has been created: " + $content.id)
while($true){
$res = Invoke-RestMethod -Method GET -uri "https://api.powerbi.com/v1.0/myorg/groups/$groupId/imports/$jobId" -UseBasicParsing -Headers $header

if($res.ImportState -ne "Publishing"){
Return "Deployment completed with the following status: " + $res.ImportState
}
Sleep -s 5
}

0 REPLIES 0

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.