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
TestCICD
Frequent Visitor

'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
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.