Friday, May 8, 2015

Deploy reports files in SSRS (Report Server) using Command Line tools (PowerShell)

In General SSRS .rdl files deployed using the below command line utilities
  • Power-Shell Scripts
  • Rs.exe Scripter (in house tool )  
Deploy SSRS files using PowerShell :

Administrators will use the command line utilities for deploy the SSRS rdl files.

Download the entire script using below link.

DeploSSRS rdl file using PowerShell with Shared Datasource

The script is used to deploy the SSRS rdl to report server.  The prerequisite for deploying reports through powershell is to set the execution policy to unrestricted.

Once Execution policy is set to Unrestricted you can run the script using powers hell.


  • Deployment and the Input parameters.




SQL Server 2005 and 2008 uses the report web service 2005 and the method name is CreateReport. for SQL Server 2008R2 on wards it uses the Report web service 2010 and the method name is CreateCatalogItem

The report shown in the example is using Shared Data source that already created in the system. 







Script  :

<#  
.DESCRIPTION
    Installs an RDL file to SQL Reporting Server using powershell
 
.NOTES
    File Name: DeploySSRS.ps1
    Prerequisite: SSRS 2008/2012/2014, Powershell 2.0
 
.PARAMETERS

     .Reporturl
        The reporturl specifies the SSRS Report manager url.
        Ex: http://bridgequiz:80/ReportServer

     .Reportfilepath
        The Report filepath is used to get the local file path of the report.
        Ex: C:\Users\Malleswara\Documents\Visual Studio 2012\Projects\ssrs\ssrs\StudentMarks.rdl".

    .ReportServerfolder
        The Reportfolder path is used to specify the report server folder in ssrs server.(report manager).
        Ex: "/Reports 2015".

    .Serverversion
        The Serverversion is to sepcify which SSRS version it is using currently in report server.
        Ex: 1) For SQLServer 2005,2008 the report serverversion is http://bridgequiz:80/ReportServer/ReportService2005.asmx?WSDL
            2) for SQLserver 2012 and 2014 the report serverversion is http://bridgequiz:80/ReportServer/ReportService2010.asmx?WSDL

     .RepName
        Name of report wherein the rdl file will be save as in Report Server.
        If this is not specified it will get the name from the file (rdl) exluding the file extension.
#>

function DeploySSRS
(
`
            [Parameter(Position = 0 )]
            [Alias("Reporturl")]
`           [string]$WebServiceUrl="http://bridgequiz:80/ReportServer",

            [Parameter(position = 1 )]
            [Alias("Reportfilepath")]
            [String]$rdlfile="C:\Users\Malleswara\Documents\Visual Studio 2012\Projects\ssrs\ssrs\StudentMarks.rdl",

            [Parameter(position = 2)]
            [Alias("ReportServerfolder")]
            [string]$reportFolder="/Reports 2015",              

    [Parameter(position = 3)]
            [Alias("Serverversion")]
            [string]$reportserverversion="2010",      

            [parameter(position = 4)]
            [Alias("RepName")]
            [string]$ReportName="",

    [Parameter(position = 5)]
    [array]$out

)

    {
                Write-Host "Hosting SSRS RDL File"
       
            # set  report server URL
       
                if ($reportserverversion -eq "2005" )
        {

           $Url = $WebServiceUrl + "/ReportService2005.asmx?WSDL"
        }
        else

        {

             $Url  = $WebServiceUrl + "/ReportService2010.asmx?WSDL"

        }


                #create SSRS proxy

                Write-Host "Creating SSRS Proxy connection to :$Url "

                try
                {

                    $ssrsproxy = New-WebserviceProxy -uri $Url  -useDefaultCredential -Namespace "ReportingWebService"


            Write-Verbose "SSRS Proxy Created Scuessfully"


                }

                catch [System.Exception]
                {

            Write-Verbose "Failed to Create SSRS Proxy "

                            $_.Exception.Message
        
                }

            # set  report name if blank , default will be the filename without extension

            if ($reportname -eq "" )
                        {

                                    $reportname = [System.IO.Path]::GetFilenameWithoutExtension($rdlFile );
             Write-Host " Report name set to $reportname"
           
         }

    try
        {

           $reports = $ssrsproxy.ListChildren("/Reports 2015", $false);

           $currreport = $reports | where { ($_.Name -eq $ReportName) }


         if ($currreport.name -eq $ReportName)
         {
              
                Write-Host "Report already exists on reportserver";
                $ssrsproxy.DeleteItem($reportFolder+"/"+$reportname);
                Write-Host "Report" $reportname "deleted sucessfully"   
         }
        
            
            Write-Host "DeploySSRS Getting file content (byte) of : $rdlFile "

            $byteArray = gc $rdlFile -encoding byte

            $msg = "DeploySSRS Total length: {0}" -f $byteArray.Length

            Write-Host $msg
 
            Write-Host "Uploading to: $reportFolder"
 
            #Call Proxy to upload report

           if ($reportserverversion -eq "2005" )
            {

                 $warnings = $ssrsProxy.CreateReport($reportName,$reportFolder,$true,$byteArray,$null)

            }
            else
            {
                [array]$test
                $ssrsProxy.CreateCatalogItem("Report", $reportname, $reportFolder, $false, $byteArray,$test , [ref]$out);


            }

            if($warnings.Length -eq $null)

                                {

                    Write-Host "report deployed Sucscessfully"
           
                 }

            else
   
                                 {
                                            $warnings | % { Write-Warning " Warning: $_"
                                 }
            }
               
    }
   
        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 while uploading rdl file : '{0}', Message: '{1}'" -f $rdlFile, $_.Exception.Detail.InnerText
            Write-Error $msg
        }
 
        }
     
       

  


No comments: