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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

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
May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.