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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register 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
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

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.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.