Skip to content

Commit bf3a122

Browse files
Rob SewellRob Sewell
authored andcommitted
added publishpbixfile
1 parent 1b757e8 commit bf3a122

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

PublishPBIXFile.ps1

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<#
2+
.SYNOPSIS
3+
Publishes a Power Bi File to a PowerBi Report Server. It will overwrite existing
4+
files.
5+
6+
.DESCRIPTION
7+
Publishes a Power Bi File to a Power Bi report Server and sets the connection
8+
string for the data source and the credential. Only works with one data source
9+
at present.It will overwrite existing files
10+
11+
.PARAMETER FolderName
12+
The Name of the folder the report is to be placed. Will be created if it doesnt
13+
exist - IE dbachecks-weekly or Finance
14+
15+
.PARAMETER ReportServerURI
16+
The URI of the Report Server like http://Server01/ReportS
17+
18+
.PARAMETER FolderLocation
19+
The Path to the folder - If it is in the root directory then / otherwise
20+
/dbachecks
21+
22+
.PARAMETER PBIXFile
23+
The full path to the pbix file
24+
25+
.PARAMETER Description
26+
The Description of the report
27+
28+
.PARAMETER ConnectionString
29+
The connection string for the data source
30+
31+
.PARAMETER ConnectionUserName
32+
The User name for the credential for the datasource if required - Use a
33+
creential object if possible
34+
35+
.PARAMETER Secret
36+
The password for the user for the datasource - Use a credential object if
37+
possible
38+
39+
.PARAMETER Credential
40+
A credential object for the user for the data source
41+
42+
.EXAMPLE
43+
44+
$FolderName = 'TestFolder'
45+
$ReportServerURI = 'http://localhost/Reports'
46+
$FolderLocation = '/'
47+
$PBIXFile = 'C:\Temp\test.pbix'
48+
$Description = "Descriptions"
49+
$ConnectionString = "Server001;dbachecks"
50+
51+
$publishPBIXFileSplat = @{
52+
ReportServerURI = $ReportServerURI
53+
FolderLocation = $FolderLocation
54+
Description = $Description
55+
PBIXFile = $PBIXFile
56+
ConnectionString = $ConnectionString
57+
FolderName = $FolderName
58+
ConnectionUserName = $UserName1
59+
Secret = $Password1
60+
61+
}
62+
Publish-PBIXFile @publishPBIXFileSplat
63+
64+
Deploys a report from the PBIX file C:\Temp\test.pbix to the report server
65+
on the localhost into a folder called TestFolder located at the root of the
66+
server (which it will create if it doesnt exist) and sets the connection
67+
string to "Server001;dbachecks" with a user name and password stored in the
68+
variables
69+
70+
.NOTES
71+
Rob Sewell 20/08/2018
72+
#>
73+
function Publish-PBIXFile {
74+
[CmdletBinding(DefaultParameterSetName = 'ByUserName', SupportsShouldProcess)]
75+
Param(
76+
[Parameter(Mandatory = $true)]
77+
[string]$FolderName,
78+
[Parameter(Mandatory = $true)]
79+
[string]$ReportServerURI,
80+
[Parameter(Mandatory = $true)]
81+
[string]$FolderLocation,
82+
[Parameter(Mandatory = $true)]
83+
[string]$PBIXFile,
84+
[Parameter()]
85+
[string]$Description = "Description of Your report Should go here",
86+
[Parameter()]
87+
[string]$ConnectionString,
88+
[Parameter(ParameterSetName = 'ByUserName')]
89+
[string]$ConnectionUserName,
90+
[Parameter(ParameterSetName = 'ByUserName')]
91+
[string]$Secret,
92+
[Parameter(Mandatory = $true, ParameterSetName = 'ByCred')]
93+
[pscredential]$Credential
94+
)
95+
96+
$FolderPath = $FolderLocation + $FolderName
97+
$PBIXName = $PBIXFile.Split('\')[-1].Replace('.pbix', '')
98+
99+
try {
100+
Write-Verbose "Creating a session to the Report Server $ReportServerURI"
101+
# establish session w/ Report Server
102+
$session = New-RsRestSession -ReportPortalUri $ReportServerURI
103+
Write-Verbose "Created a session to the Report Server $ReportServerURI"
104+
}
105+
catch {
106+
Write-Warning "Failed to create a session to the report server $reportserveruri"
107+
Return
108+
}
109+
110+
# create folder (optional)
111+
try {
112+
if ($PSCmdlet.ShouldProcess("$ReportServerURI","Creating a folder called $FolderName at $FolderLocation")){
113+
$Null = New-RsRestFolder -WebSession $session -RsFolder $FolderLocation -FolderName $FolderName -ErrorAction Stop
114+
}
115+
}
116+
catch [System.Exception] {
117+
If ($_.Exception.InnerException.Message -eq 'The remote server returned an error: (409) Conflict.') {
118+
Write-Warning "The folder already exists - moving on"
119+
}
120+
}
121+
catch {
122+
Write-Warning "Failed to create a folder called $FolderName at $FolderLocation report server $ReportServerURI but not because it already exists"
123+
Return
124+
}
125+
126+
try {
127+
if ($PSCmdlet.ShouldProcess("$ReportServerURI","Uploading the pbix from $PBIXFile to the report server ")){
128+
# upload copy of PBIX to new folder
129+
Write-RsRestCatalogItem -WebSession $session -Path $PBIXFile -RsFolder $folderPath -Description $Description -Overwrite
130+
}
131+
}
132+
catch {
133+
Write-Warning "Failed to upload the file $PBIXFile to report server $ReportServerURI"
134+
Return
135+
}
136+
137+
try {
138+
Write-Verbose "Getting the datasources from the pbix file for updating"
139+
# get data source object
140+
$dataSource = Get-RsRestItemDataSource -WebSession $session -RsItem "$FolderPath/$PBIXName"
141+
Write-Verbose "Got the datasources for updating"
142+
}
143+
catch {
144+
Write-Warning "Failed to get the datasources"
145+
Return
146+
}
147+
148+
149+
try {
150+
Write-Verbose "Updating Datasource"
151+
# change connection string (to point at new source)
152+
$dataSource.ConnectionString = $ConnectionString
153+
154+
if ($Credential -or $UserName) {
155+
if ($Credential) {
156+
$UserName = $Credential.UserName
157+
$Password = $Credential.GetNetworkCredential().Password
158+
}
159+
else {
160+
$UserName = $ConnectionUserName
161+
$Password = $Secret
162+
}
163+
$dataSource.CredentialRetrieval = 'Store'
164+
$dataSource.DataModelDataSource.Username = $UserName
165+
$dataSource.DataModelDataSource.Secret = $Password
166+
}
167+
if ($PSCmdlet.ShouldProcess("$ReportServerURI","Updating the data source for the report $PBIXName")){
168+
# update data source object on server
169+
Set-RsRestItemDataSource -WebSession $session -RsItem "$folderPath/$PBIXName" -RsItemType PowerBIReport -DataSources $datasource
170+
}
171+
}
172+
catch {
173+
Write-Warning "Failed to set the datasource"
174+
Return
175+
}
176+
Write-Verbose "Completed Successfully"
177+
}

0 commit comments

Comments
 (0)