Skip to content

Instantly share code, notes, and snippets.

@okpedro
Created December 8, 2020 16:09
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save okpedro/1b185cafae60325d18b745ae9a07fefb to your computer and use it in GitHub Desktop.
# USE THIS SCRIPT TO UPLOAD LARGE .PBIX FILES TO POWER BI REPORT SERVER
#
# I needed to manually construct the form-data as PowerShell doesn't appear to have full support for
# this yet, though it appears to be coming soon.
#CREDENTIALS
[string]$userName = 'BIWIN\MS'
[string]$userPassword = 'xxxxxxxxxxxxx'
[securestring]$secStringPassword = ConvertTo-SecureString $userPassword -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)
# REPORT PORTAL E.G. 'http://pbirs.com/Reports'
$ReportPortal = "http://biwin-y/Reports"
# REPORT NAME E.G. 'MyReport'
$ReportName = "MyReport"
# REPORT TARGET PATH INCLUDING REPORTNAME E.G. '/PowerBI/Finance/MyReport'
$ReportTargetPath = "/PowerBI/Finance/MyReport"
# FULL PATH OF FILE TO UPLOAD E.G. 'C:\Users\Pedro\ReportToUpload.pbix'
$FileToUpload = "C:\Users\Pedro\ReportToUpload.pbix"
# EDIT PARAMETERS ABOVE THIS LINE ONLY
$ReportTargetPathEncoded = ("%27", $ReportTargetPath, "%27") -join ""
$bytes = [System.IO.File]::ReadAllBytes($FileToUpload)
$pbixPayload = [System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($bytes);
$endpoint = $ReportPortal + "/api/v2.0/PowerBIReports(Path=$ReportTargetPathEncoded)/Model.Upload"
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"
$bodyLines = (
# Name
"--$boundary",
"Content-Disposition: form-data; name=`"Name`"$LF",
$ReportName,
# ContentType
"--$boundary",
"Content-Disposition: form-data; name=`"ContentType`"$LF",
"",
# Content
"--$boundary",
"Content-Disposition: form-data; name=`"Content`"$LF",
"undefined",
# Path
"--$boundary",
"Content-Disposition: form-data; name=`"Path`"$LF",
$ReportTargetPath,
# @odata.type
"--$boundary",
"Content-Disposition: form-data; name=`"@odata.type`"$LF",
"#Model.PowerBIReport",
# File
"--$boundary",
"Content-Disposition: form-data; name=`"File`"; filename=`"$ReportName`"",
"Content-Type: application/octet-stream$LF",
$pbixPayload,
"--$boundary--"
) -join $LF
Invoke-RestMethod `
-Uri $endPoint `
-Body $bodyLines `
-Method POST `
-Credential $Credential `
-ContentType "multipart/form-data; boundary=$boundary" `
-Headers @{
"accept"="application/json, text/plain, */*"
"accept-language"="en-US,en;q=0.9"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment