Last active
December 5, 2017 21:53
-
-
Save dudelis/0ac8bc4e0deb9b796f5b25a0b1c098d9 to your computer and use it in GitHub Desktop.
SSRS - Deployment of all the Reports, DataSources, DataSets and fixing the DataSoure bindings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Deploy-SSRSProject([string]$ssrsServer, [string]$rdlPath, [string]$ssrsFolder, [string]$schema = "https"){ | |
#Set variables with configure values | |
$reportPath ="/" # Rool Level | |
$DataSourcePath = "/Data Sources" #Folder where we need to create DAtasource | |
$DataSet_Folder = "/Datasets" # Folder where we need to create DataSet | |
$IsOverwriteDataSource = 0 | |
$IsOverwriteDataSet = 1 | |
$IsOverwriteReport = 1 | |
$webServiceUrl = $schema + "://$ssrsServer" | |
#Connecting to SSRS | |
Write-Host "Reportserver: $webServiceUrl" -ForegroundColor Magenta | |
Write-Host "Creating Proxy, connecting to : $webServiceUrl/ReportServer/ReportService2010.asmx?WSDL" | |
Write-Host "" | |
$ssrsProxy = New-WebServiceProxy -Uri $webServiceUrl'/ReportServer/ReportService2010.asmx?WSDL' -UseDefaultCredential | |
$reportFolder_Final = $reportPath + $ssrsFolder | |
########################################## | |
#Create Report Folder | |
Write-host "" | |
try { | |
$ssrsProxy.CreateFolder($ssrsFolder, $reportPath, $null) | |
Write-Host "Created new folder: $reportFolder_Final" | |
} | |
catch [System.Web.Services.Protocols.SoapException] { | |
if ($_.Exception.Detail.InnerText -match "[^rsItemAlreadyExists400]") { | |
Write-Host "Folder: $ssrsFolder already exists." | |
} | |
else { | |
$msg = "Error creating folder: $ssrsFolder. Msg: '{0}'" -f $_.Exception.Detail.InnerText | |
Write-Error $msg | |
} | |
} | |
########################################## | |
#Create datasource | |
foreach($rdsfile in Get-ChildItem $rdlPath -Filter *.rds) | |
{ | |
Write-host $rdsfile | |
#create data source | |
try { | |
$rdsf = [System.IO.Path]::GetFileNameWithoutExtension($rdsfile); | |
$RdsPath = $rdlPath+"\"+$rdsf+".rds" | |
Write-host "Reading data from $RdsPath" | |
[xml]$Rds = Get-Content -Path $RdsPath | |
$ConnProps = $Rds.RptDataSource.ConnectionProperties | |
$type = $ssrsProxy.GetType().Namespace | |
$datatype = ($type + '.DataSourceDefinition') | |
$datatype_Prop = ($type + '.Property') | |
$DescProp = New-Object($datatype_Prop) | |
$DescProp.Name = 'Description' | |
$DescProp.Value = '' | |
$HiddenProp = New-Object($datatype_Prop) | |
$HiddenProp.Name = 'Hidden' | |
$HiddenProp.Value = 'false' | |
$Properties = @($DescProp, $HiddenProp) | |
$Definition = New-Object ($datatype) | |
$Definition.ConnectString = $ConnProps.ConnectString | |
$Definition.Extension = $ConnProps.Extension | |
if ([Convert]::ToBoolean($ConnProps.IntegratedSecurity)) { | |
$Definition.CredentialRetrieval = 'Integrated' | |
} | |
$DataSource = New-Object -TypeName PSObject -Property @{ | |
Name = $Rds.RptDataSource.Name | |
Path = $Folder + '/' + $Rds.RptDataSource.Name | |
} | |
if ($IsOverwriteDataSource -eq 1) { | |
[boolean]$IsOverwriteDataSource = 1 | |
} | |
else { | |
[boolean]$IsOverwriteDataSource = 0 | |
} | |
$warnings = $ssrsProxy.CreateDataSource($rdsf, $DataSourcePath, $IsOverwriteDataSource, $Definition, $Properties) | |
# Write-Host $warnings | |
} | |
catch [System.IO.IOException] { | |
$msg = "Error while reading rds file : '{0}', Message: '{1}'" -f $rdsfile, $_.Exception.Message | |
Write-Error msgcler | |
} | |
catch [System.Web.Services.Protocols.SoapException] { | |
if ($_.Exception.Detail.InnerText -match "[^rsItemAlreadyExists400]") { | |
Write-Host "DataSource: $rdsf already exists." | |
} | |
else { | |
$msg = "Error uploading report: $rdsf. Msg: '{0}'" -f $_.Exception.Detail.InnerText | |
Write-Error $msg | |
} | |
} | |
} | |
############################# | |
#For each RDL file in | |
foreach($rdlfile in Get-ChildItem $rdlPath -Filter *.rdl){ | |
Write-host "" | |
#ReportName | |
$reportName = [System.IO.Path]::GetFileNameWithoutExtension($rdlFile); | |
write-host $reportName -ForegroundColor Green | |
#Upload File | |
try { | |
#Get Report content in bytes | |
Write-Host "Getting file content of : $rdlFile" | |
$byteArray = gc $rdlFile.FullName -encoding byte | |
$msg = "Total length: {0}" -f $byteArray.Length | |
Write-Host $msg | |
Write-Host "Uploading to: $reportFolder_Final" | |
$type = $ssrsProxy.GetType().Namespace | |
$datatype = ($type + '.Property') | |
$DescProp = New-Object($datatype) | |
$DescProp.Name = 'Description' | |
$DescProp.Value = '' | |
$HiddenProp = New-Object($datatype) | |
$HiddenProp.Name = 'Hidden' | |
$HiddenProp.Value = 'false' | |
$Properties = @($DescProp, $HiddenProp) | |
#Call Proxy to upload report | |
$warnings = $null | |
$Results = $ssrsProxy.CreateCatalogItem("Report", $reportName,$reportFolder_Final, $IsOverwriteReport,$byteArray,$Properties,[ref]$warnings) | |
if($warnings.length -le 1) { | |
Write-Host "Upload Success." -ForegroundColor Green | |
} | |
else { | |
write-host $warnings | |
} | |
} | |
catch [System.IO.IOException] | |
{ | |
$msg = "Error while reading rdl file : '{0}', Message: '{1}'" -f $rdlFile, $_.Exception.Message | |
Write-Error msg | |
} | |
catch [System.Web.Services.Protocols.SoapException] { | |
$msg = "Error uploading report: $reportName. Msg: '{0}'" -f $_.Exception.Detail.InnerText | |
Write-Error $msg | |
} | |
########################################## | |
##Change Datasource | |
$reportFullName = $reportFolder_Final+"/"+$reportName | |
Write "Datasource record $reportFullName" | |
$rep = $ssrsProxy.GetItemDataSources($reportFullName) | |
$rep | ForEach-Object { | |
$proxyNamespace = $_.GetType().Namespace | |
$constDatasource = New-Object ("$proxyNamespace.DataSource") | |
$constDatasource.Item = New-Object ("$proxyNamespace.DataSourceReference") | |
$FinalDatasourcePath = $DataSourcePath+"/" + $($_.Name) | |
$constDatasource.Item.Reference = $FinalDatasourcePath | |
$_.item = $constDatasource.Item | |
$ssrsProxy.SetItemDataSources($reportFullName, $_) | |
Write-Host "Changing datasource `"$($_.Name)`" to $($_.Item.Reference)" | |
} | |
} | |
Write-host "`n SSRS Project was successfully deployed" -ForegroundColor Magenta | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment