<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Sample publish script in Powershell? in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Sample-publish-script-in-Powershell/m-p/417649#M12426</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have a sample script for publishing a PBIX to the Power BI Service via the REST API?&lt;/P&gt;&lt;P&gt;I have found the reference on&amp;nbsp;&lt;A href="https://msdn.microsoft.com/en-us/library/mt243840.aspx" target="_blank"&gt;https://msdn.microsoft.com/en-us/library/mt243840.aspx&lt;/A&gt;, but can not get it to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope someone can help me!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards, Arie&lt;/P&gt;</description>
    <pubDate>Tue, 15 May 2018 11:08:03 GMT</pubDate>
    <dc:creator>Arie</dc:creator>
    <dc:date>2018-05-15T11:08:03Z</dc:date>
    <item>
      <title>Sample publish script in Powershell?</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Sample-publish-script-in-Powershell/m-p/417649#M12426</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have a sample script for publishing a PBIX to the Power BI Service via the REST API?&lt;/P&gt;&lt;P&gt;I have found the reference on&amp;nbsp;&lt;A href="https://msdn.microsoft.com/en-us/library/mt243840.aspx" target="_blank"&gt;https://msdn.microsoft.com/en-us/library/mt243840.aspx&lt;/A&gt;, but can not get it to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope someone can help me!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards, Arie&lt;/P&gt;</description>
      <pubDate>Tue, 15 May 2018 11:08:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Sample-publish-script-in-Powershell/m-p/417649#M12426</guid>
      <dc:creator>Arie</dc:creator>
      <dc:date>2018-05-15T11:08:03Z</dc:date>
    </item>
    <item>
      <title>Re: Sample publish script in Powershell?</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Sample-publish-script-in-Powershell/m-p/419146#M12501</link>
      <description>&lt;P&gt;The only PowerShell that currently avaialble to find under Github is the following:&lt;/P&gt;&lt;P&gt;&lt;SPAN class="repo-root js-repo-root"&gt;&lt;SPAN class="js-path-segment"&gt;&lt;A href="https://github.com/Azure-Samples/powerbi-powershell" target="_blank"&gt;&lt;SPAN&gt;powerbi-powershell&lt;/SPAN&gt;&lt;/A&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="separator"&gt;/&lt;/SPAN&gt;&lt;STRONG&gt;copyWorkspace.ps1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which copies the workspace, and then export/Import the PBIX file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the Export and import part, check:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;# PART 3: Copying reports and datasets using Export/Import PBIX APIs&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In the scripts, import part should be in&amp;nbsp;Bold :&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;# PART 3: Copying reports and datasets using Export/Import PBIX APIs
# ==================================================================
$report_ID_mapping = @{}      # mapping of old report ID to new report ID
$dataset_ID_mapping = @{}     # mapping of old model ID to new model ID
$failure_log = @()  
$import_jobs = @()
$source_group_path = get_groups_path($source_group_ID)
$target_group_path = get_groups_path($target_group_ID)

$uri = "https://api.powerbi.com/v1.0/$source_group_path/reports/"
$reports_json = Invoke-RestMethod -Uri $uri –Headers $auth_header –Method GET
$reports = $reports_json.value

# For My Workspace, filter out reports that I don't own - e.g. those shared with me
if ($source_group_ID -eq "me") {
    $reports_temp = @()
    Foreach($report in $reports) {
        if ($report.isOwnedByMe -eq "True") {
            $reports_temp += $report
        }
    }
    $reports = $reports_temp
}

# == Export/import the reports that are built on PBIXes (this step creates the datasets)
# for each report, try exporting and importing the PBIX
New-Item -Path $temp_path_root -ItemType Directory 
"=== Exporting PBIX files to copy datasets..."
Foreach($report in $reports) {
   
    $report_id = $report.id
    $dataset_id = $report.datasetId
    $report_name = $report.name
    $temp_path = "$temp_path_root\$report_name.pbix"

    # only export if this dataset hasn't already been seen
    if ($dataset_ID_mapping[$dataset_id]) {
        continue
    }

    "== Exporting $report_name with id: $report_id to $temp_path"
    $uri = "https://api.powerbi.com/v1.0/$source_group_path/reports/$report_id/Export"
    try {
        Invoke-RestMethod -Uri $uri –Headers $auth_header –Method GET -OutFile "$temp_path"
    } catch {
        Write-Host "= This report and dataset cannot be copied, skipping. This is expected for most workspaces."
        continue
    }
     
    try {
        "== &lt;STRONG&gt;Importing $report_name to target workspace"
        $uri = "https://api.powerbi.com/v1.0/$target_group_path/imports/?datasetDisplayName=$report_name.pbix&amp;amp;nameConflict=Abort"

        # 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", $token.AccessToken);
        $packageFileStream = New-Object System.IO.FileStream @($temp_path, [System.IO.FileMode]::Open)
        
	    $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)

	    $response = $httpClient.PostAsync($Uri, $content).Result
 
	    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
		} 
        
        # save the import IDs
        $import_job_id = (ConvertFrom-JSON($response.Content.ReadAsStringAsync().Result)).id

        # wait for import to complete
        $upload_in_progress = $true
        while($upload_in_progress) {
            $uri = "https://api.powerbi.com/v1.0/$target_group_path/imports/$import_job_id"
            $response = Invoke-RestMethod -Uri $uri –Headers $auth_header –Method GET
            
            if ($response.importState -eq "Succeeded") {
                "Publish succeeded!"
                # update the report and dataset mappings
                $report_id_mapping[$report_id] = $response.reports[0].id
                $dataset_id_mapping[$dataset_id] = $response.datasets[0].id
                break
            }

            if ($response.importState -ne "Publishing") {
                "Error: publishing failed, skipping this. More details: "
                $response
                break
            }
            
            Write-Host -NoNewLine "."
            Start-Sleep -s 5
        }
            
        
    } catch [Exception] {
        Write-Host $_.Exception
	    Write-Host "== Error: failed to import PBIX"
        Write-Host "= HTTP Status Code:" $_.Exception.Response.StatusCode.value__ 
        Write-Host "= HTTP Status Description:" $_.Exception.Response.StatusDescription
        continue
    }&lt;/STRONG&gt;
}&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Michael&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 May 2018 02:35:59 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Sample-publish-script-in-Powershell/m-p/419146#M12501</guid>
      <dc:creator>v-micsh-msft</dc:creator>
      <dc:date>2018-05-17T02:35:59Z</dc:date>
    </item>
  </channel>
</rss>

