Microsoft Fabric Community Conference 2025, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount.
Register nowThe Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.
I Need to upload the 1.5 gb file into powebi online. So I am using a PowerShell script to create the temporary storage, and then push\import the actual work space.
Below is the script which I have used to connect the powerbi online using PowerShell script. Can anyone help me to upload the large .pbix file into powebi online, as I am facing difficulties to set up the proper parameters for the power bi API to upload the large .pbix file into the temporary storage and then sync \import to the work space.
Param(
[Parameter(Mandatory=$true)]
[string]$tenantId = $(throw 'The tenantId should be provided'),
[Parameter(Mandatory=$true)]
[string]$clientId = $(throw 'The clientId should be provided'),
[Parameter(Mandatory=$true)]
[string] $clientSecret = $(throw 'The clientSecret should be provided'),
[Parameter(Mandatory=$true)]
[string]$groupId = $(throw 'The groupId should be provided')
)
#$tenantId = 'd693e859-2df6-4cc4-94b8-1107a791196a';
Write-Host *********************** Executing with following parameters ***********************
Write-Host ' $tenantId :' $tenantId
Write-Host ' $clientId :' $clientId
Write-Host ' $clientSecret : *******************'
Write-Host ' $groupId :' $groupId
Write-Host ' Executing in :' $ENV:COMPUTERNAME
Write-Host ' Executing at :' (Get-date).ToUniversalTime() '(Universal Time)'
Write-Host ***********************************************************************************
Write-Host
$tenantId = $tenantId.Trim();
$clientId = $clientId.Trim();
$clientSecret = $clientSecret.Trim();
$groupId = $groupId.Trim();
$indent = ' ';
if($tenantId -eq ''){
$(throw "Empty tenantId is not allowed.")
}
elseif($clientId -eq ''){
$(throw "Empty clientId is not allowed.")
}
elseif($storageAccountName -eq ''){
$(throw "Empty storageAccountName is not allowed.")
}
elseif($clientSecret -eq ''){
$(throw "Empty clientSecret is not allowed.")
}
elseif($groupId -eq ''){
$(throw "Empty groupId is not allowed.")
}
else{
try{
if (Get-Module -ListAvailable -Name MicrosoftPowerBIMgmt) {
Write-Host $indent'=> MicrosoftPowerBIMgmt module already installed';
}
else {
Write-Host $indent'=> Installing MicrosoftPowerBIMgmt module';
Install-Module MicrosoftPowerBIMgmt -Force -Scope "CurrentUser"
Write-Host $indent'=> Installed MicrosoftPowerBIMgmt module';
}
if (Get-Module -ListAvailable -Name MicrosoftPowerBIMgmt.Profile) {
Write-Host $indent'=> MicrosoftPowerBIMgmt.Profile module already installed';
}
else {
Write-Host $indent'=> Installing MicrosoftPowerBIMgmt.Profile module';
Install-Module MicrosoftPowerBIMgmt.Profile -Force -Scope "CurrentUser"
Write-Host $indent'=> Installed MicrosoftPowerBIMgmt.Profile module';
}
Import-Module MicrosoftPowerBIMgmt
Import-Module MicrosoftPowerBIMgmt.Profile
Disconnect-PowerBIServiceAccount
#$clientSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force ;
$spCredentials =New-Object System.Management.Automation.PSCredential($clientId, ( ConvertTo-SecureString $clientSecret -AsPlainText -Force)) ;
Write-Host $indent'=> Created credentials for Service Principal';
Login-PowerBI -ServicePrincipal -TenantId $tenantId -Credential $spCredentials -ErrorAction: Stop;
Write-Host $indent'=> Logged into power BI with Service Principal';
#setting headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization' , (Get-PowerBIAccessToken -AsString));
#$headers.Add('Accept' , '*/*');
Write-Host $indent'=> Set Request Headers with Bearer Token.';
$bodyJson = '{
}';
Write-Host $indent'=> Constructed the Request body.';
$url ='https://api.powerbi.com/v1.0/myorg/groups/'+$groupId+'/imports/createTemporaryUploadLocation'
#$pbiurl ='groups/0dc4fca1-f34b-4765-86fa-e0c47bbd39f1/imports/createTemporaryUploadLocation'
Write-Host $indent'=> Constructed the url.';
$tulResponse = Invoke-RestMethod -Uri:$url -Method:Post -Body:$bodyJson -Headers:$headers -ContentType:'application/json; charset=utf-8' -Verbose
#Invoke-PowerBIRestMethod -Url:$pbiurl -Method:Post -Body:$bodyJson -Headers:$headers -ContentType:'application/json; charset=utf-8' -Verbose
Write-host '$tulResponse. url:'$tulResponse.url
#Write-host '$tulResponse. expirationTime:'$tulResponse.expirationTime
Write-Host $indent'=> Created the Temporary Storage Location.';
#$encodedUrl = [System.Web.HTTPUtility]::UrlEncode($tulResponse.url).Replace('&','"&"');
#Write-Host $indent'=> $encodedUrl: ' $encodedUrl
#Write-Host "##vso[task.setvariable variable=SASUrl;]$encodedUrl"
#Write-Host $indent'=> Set Task Variable SASUrl => '
#Write-Host "##vso[task.setvariable variable=SASExpirationTime;]$tulResponse.expirationTime"
#Write-Host $indent'=> Set Task Variable SASExpirationTime'
Write-Host '==========================='
$file = "C:\Downloads\Asset Walk_1.pbix"
#Get the File-Name without path
$name = (Get-Item $file).Name
#The target URL wit SAS Token
$uri = $tulResponse.url
#"https://test.blob.core.windows.net/logs/$($name)?st=2019-04-03T07%3A28%3A36Z&se=2019-04-03T07%3A28%3..."
#Define required Headers
$headers = @{
'x-ms-blob-type' = 'BlockBlob'
}
#Upload File...
$response = Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -InFile $file
$response
Write-host ' uploaded the file into temporary storage'
<#
$Path = 'C:\Downloads\Asset Walk_1.pbix'
$powerBiBodyTemplate = @'
--{0}
Content-Disposition: form-data; name="fileData"; filename="{1}"
Content-Type: text/xml
{2}
--{0}--
'@
write-host ''
$fileName = [IO.Path]::GetFileName($Path)
$boundary = [guid]::NewGuid().ToString()
Write-Host ' before fileBytes'
$fileBytes = [System.IO.File]::ReadAllBytes($Path)
Write-Host ' after fileBytes'
$encoding = [System.Text.Encoding]::GetEncoding("utf-8")
$filebody = $encoding.GetString($fileBytes)
Write-Host ' after filebody call'
$body = $powerBiBodyTemplate -f $boundary, $fileName, $encoding.GetString($fileBytes)
$body
$headers = Get-PowerBIAccessToken
if ($GroupId) {
$url = "https://api.powerbi.com/v1.0/myorg/groups/$GroupId/imports?datasetDisplayName=$fileName&nameConflict..."
}
else {
$url = "https://api.powerbi.com/v1.0/myorg/imports?datasetDisplayName=$fileName&nameConflict=CreateOrOverwri..."
}
Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body -ContentType "multipart/form-data; boundary=--$boundary" | Out-Null
Write-Host "Imported Power BI file $Path"
#>
<#
$bodyJson = '{
}';
Write-Host $indent'=> Constructed the Request body.';
$reportFileName = 'Asset Walk.pbix';
$url = 'https://api.powerbi.com/v1.0/myorg/groups/'+$groupId+'/imports?datasetDisplayName='+$reportFileName+...';
# Here we switch to HttpClient class to help POST the form data for importing PBIX
$httpClient = New-Object System.Net.Http.Httpclient $httpClientHandler
$httpClient.DefaultRequestHeaders.Authorization = New-Object System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", (Get-PowerBIAccessToken -AsString));
$file_name = 'C:\Downloads\Asset Walk_1.pbix';
$packageFileStream = New-Object System.IO.FileStream @($file_name, [System.IO.FileMode]::Open)
Write-Host '=> Set Package file stream';
$contentDispositionHeaderValue = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue "form-data"
$contentDispositionHeaderValue.Name = "file0"
$contentDispositionHeaderValue.FileName = $file_name
$streamContent = New-Object System.Net.Http.StreamContent $packageFileStream
$streamContent.Headers.ContentDisposition = $contentDispositionHeaderValue
$content = New-Object System.Net.Http.MultipartFormDataContent
$content.Add($streamContent)
Write-Host '=> Before Post $url:' $url;
$response = $httpClient.PostAsync($url, $content).Result
$response;
if (!$response.IsSuccessStatusCode) {
$responseBody = $response.Content.ReadAsStringAsync().Result
"= This report cannot be imported to target workspace. Skipping..."
$errorMessage = "Status code {0}. Reason {1}. Server reported the following message: {2}." -f $response.StatusCode, $response.ReasonPhrase, $responseBody
throw [System.Net.Http.HttpRequestException] $errorMessage
}
#>
Write-Host *********************** Execution Completed ***********************
}
catch{
Write-Error ' Inside Catch '
Write-Host $_
Write-Error ''
Write-Error $_.Exception.InnerException
Write-Error ''
#Write-Error $StatusCode = $_.Exception.Response.StatusCode.value__;
throw
}
}
function Publish-PowerBIImport {
param
(
[string]$Path,
[string]$GroupId
)
$powerBiBodyTemplate = @'
--{0}
Content-Disposition: form-data; name="fileData"; filename="{1}"
Content-Type: text/xml
{2}
--{0}--
'@
$fileName = [IO.Path]::GetFileName($Path)
$boundary = [guid]::NewGuid().ToString()
$fileBytes = [System.IO.File]::ReadAllBytes($Path)
$encoding = [System.Text.Encoding]::GetEncoding("utf-8")
$filebody = $encoding.GetString($fileBytes)
$body = $powerBiBodyTemplate -f $boundary, $fileName, $encoding.GetString($fileBytes)
$headers = Get-PowerBIAccessToken
if ($GroupId) {
$url = "https://api.powerbi.com/v1.0/myorg/groups/$GroupId/imports?datasetDisplayName=$fileName&nameConflict..."
}
else {
$url = "https://api.powerbi.com/v1.0/myorg/imports?datasetDisplayName=$fileName&nameConflict=CreateOrOverwri..."
}
Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body -ContentType "multipart/form-data; boundary=--$boundary" | Out-Null
Write-Host "Imported Power BI file $Path"
}
Hi,
I am not very good at PowerShell but hope this helps:
https://www.reddit.com/r/PowerBI/comments/bgezzd/import_pbix_file_to_powerbi_using_powershell/
Best Regards,
Giotto
Ok, Thanks for your reply.
I have gone through this URL, and this script is to upload a .pbix file into the workspace. But if the file size exceeds 1 GB, then we can not use this function, instead, we have to create the temporary storage location and upload the report to this temporary location. with https://docs.microsoft.com/en-us/rest/api/power-bi/imports/createtemporaryuploadlocation
Then we have to call the Post Import . I am facing issue to create the request to upload the .pbix file into the temporary location using shared access signature (SAS) url
@Anonymous Any luck getting it right? No example is frustrating!
User | Count |
---|---|
45 | |
26 | |
21 | |
18 | |
18 |
User | Count |
---|---|
53 | |
45 | |
25 | |
24 | |
21 |