-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathReport-PublishReport.ps1
87 lines (57 loc) · 2.97 KB
/
Report-PublishReport.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#Requires -Modules @{ ModuleName="MicrosoftPowerBIMgmt"; ModuleVersion="1.2.1026" }
param(
[string]$path = ".\SampleReport - Live.pbix",
[string]$workspaceId = "00fad993-e80b-45b1-9376-0a1e637cafa9",
[string]$datasetId,
[bool]$handleReportDuplication = $true
)
$ErrorActionPreference = "Stop"
$currentPath = (Split-Path $MyInvocation.MyCommand.Definition -Parent)
Set-Location $currentPath
Connect-PowerBIServiceAccount
$reports = Get-ChildItem -File -Path $path -ErrorAction SilentlyContinue
foreach($pbixFile in $reports)
{
Write-Host "Deploying report: '$($pbixFile.Name)'"
$filePath = $pbixFile.FullName
$reportName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
$targetReport = @(Get-PowerBIReport -WorkspaceId $workspaceId -Name $reportName)
if ($targetReport.Count -eq 0 -or !$handleReportDuplication)
{
Write-Host "Uploading new report to workspace '$workspaceId'"
$importResult = New-PowerBIReport -Path $filePath -WorkspaceId $workspaceId -Name $reportName -ConflictAction CreateOrOverwrite
$targetReportId = $importResult.Id
}
elseif ($handleReportDuplication)
{
if ($targetReport.Count -gt 1)
{
throw "More than one report with name '$reportName', please remove and keep only one"
}
Write-Host "Report already exists on workspace '$workspaceId', uploading to temp report & updatereportcontent"
$targetReport = $targetReport[0]
$targetReportId = $targetReport.id
# Upload a temp report and update the report content of the target report
# README - This is required because of a limitation of Import API that always duplicate the report if the dataset is different (may be solved in the future)
$tempReportName = "Temp_$([System.Guid]::NewGuid().ToString("N"))"
Write-Host "Uploadind as a temp report '$tempReportName'"
$importResult = New-PowerBIReport -Path $filePath -WorkspaceId $workspaceId -Name $tempReportName -ConflictAction Abort
$tempReportId = $importResult.Id
Write-Host "Updating report content"
$updateContentResult = Invoke-PowerBIRestMethod -method Post -Url "groups/$workspaceId/reports/$targetReportId/UpdateReportContent" -Body (@{
sourceType = "ExistingReport"
sourceReport = @{
sourceReportId = $tempReportId
sourceWorkspaceId = $workspaceId
}
} | ConvertTo-Json)
# Delete the temp report
Write-Host "Deleting temp report '$tempReportId'"
Invoke-PowerBIRestMethod -Method Delete -Url "groups/$workspaceId/reports/$tempReportId"
}
if ($targetReportId -and $dataSetId)
{
Write-Host "Rebinding to dataset '$dataSetId'"
Invoke-PowerBIRestMethod -Method Post -Url "groups/$workspaceId/reports/$targetReportId/Rebind" -Body "{datasetId: '$dataSetId'}" | Out-Null
}
}