#============================================================== #VANPASS August 2011 #SSRS Beyond 101 #http://www.sqlmusings.com / http://www.twitter.com/sqlbelle http://www.queryworks.ca #============================================================== #-------------------------------------------------------------- #Approach 1: simplistic approach - accessing report via URL #-------------------------------------------------------------- #Set-Alias ie "$env:programfiles\Internet Explorer\iexplore.exe" #note you can control how the report is rendered via the parameters in the URL string #ie "http://localhost/ReportServer_SQL01/Pages/ReportViewer.aspx?%2fReports%2fSample+Report&rs:Command=Render" #-------------------------------------------------------------- #Approach 2: using the ReportViewer #You need to download the ReportViewer redistributable package #-------------------------------------------------------------- #I am qualifying this because I have more than one version in my system [void] [System.Reflection.Assembly]::Load("Microsoft.ReportViewer.WinForms, ` Version=10.0.0.0, Culture=neutral, ` PublicKeyToken=b03f5f7f11d50a3a") #If you need webforms, use Microsoft.ReportViewer.WebForms #Windows.Forms for viewing [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") #System.IO because I want to export to Excel [void][System.Reflection.Assembly]::LoadWithPartialName("System.IO") #System.IO because I want to open Acrobat [void][System.Reflection.Assembly]::LoadWithPartialName("System.Diagnostics") [void][System.Reflection.Assembly]::LoadWithPartialName("System.Net") $rv = New-Object Microsoft.Reporting.WinForms.ReportViewer; $rv.ProcessingMode = "Remote"; $rv.ServerReport.ReportServerUrl = "http://localhost/ReportServer_SQL01"; $rv.ServerReport.ReportPath = "/VANPASS Reports/01 - Invoices"; #if you need to provide basic credentials, use the following #$rv.ServerReport.ReportServerCredentials.NetworkCredentials= New-Object System.Net.NetworkCredential("sqladmin", "P@assword"); $rv.Height = 800; $rv.Width = 1200; $rv.RefreshReport(); #-------------------------------------------------------------- #Windows Form #-------------------------------------------------------------- #create a new form $form = New-Object Windows.Forms.Form; #we're going to make it just slightly bigger than $form.Height = 810; $form.Width= 1210; $form.Controls.Add($rv); $rv.Show(); $form.ShowDialog(); #-------------------------------------------------------------- #Excel #-------------------------------------------------------------- #now let's try exporting to Excel $mimeType = $null; $encoding = $null; $extension = $null; $streamids = $null; $warnings = $null; $bytes = $rv.ServerReport.Render("Excel", $null, [ref] $mimeType, [ref] $encoding, [ref] $extension, [ref] $streamids, [ref] $warnings); $file = "C:\Presentations\VANPASS\samplereport.xls"; $fileStream = New-Object System.IO.FileStream($file, [System.IO.FileMode]::OpenOrCreate); $fileStream.Write($bytes, 0, $bytes.Length); $fileStream.Close(); #let's open up our excel application $excel = New-Object -comObject Excel.Application $excel.visible = $true; $excel.Workbooks.Open($file) | Out-Null; #-------------------------------------------------------------- #PDF #-------------------------------------------------------------- $bytes = $rv.ServerReport.Render("PDF", $null, [ref] $mimeType, [ref] $encoding, [ref] $extension, [ref] $streamids, [ref] $warnings); $file2 = "C:\Presentations\VANPASS\samplereport.pdf"; $fileStream = New-Object System.IO.FileStream($file2, [System.IO.FileMode]::OpenOrCreate); $fileStream.Write($bytes, 0, $bytes.Length); $fileStream.Close(); [System.Diagnostics.Process]::Start($file2) #-------------------------------------------------------------- #Other ways to render or access your reports: #SSRS Web Services #--------------------------------------------------------------