Send Powershell output to an Excel, PDF, or Image file

While reading through Wrox book, Professional SQL Server 2008 Reporting Services (SSRS) I noticed a chapter on "Integrating Reports Into Custom Applicaiton" where the authors make use of the ReportViewer control to embed an SSRS report in a WinForm application without needing an SSRS server. Since SSRS reports can use DataTables as a data sources I thought this would make a nice way to export the output of any Powershell command/script to a number of different formats natively supported by SSRS including Excel, PDF, and various image formats: BMP,EMF,GIF,JPEG,PNG or TIFF.
 
In Searching for examples of ReportViewer WinForm implementations I found a very nice set of classes called ReportExporters by Andriy Protskiv which greatly simplify working with ReportViewer. Using ReportExporters I created an Out-Report script available on Poshcode. To use you’ll need to install the Microsoft Report Viewer Redistributable, either the 2005 or 2008 version will work. I think Visual Studio and/or SSRS server includes the ReportViewer. I have both installed on my machine and did not need to install the Redistributable. You will also need to download or compile the ReportExporters dlls. You can grab the compiled version from demo code in the article.
 
Here are few examples of what you can do with the script:
 
get-alias | ./out-report.ps1 "c:usersu00documentsaliases.xls" xls
get-alias | ./out-report.ps1 "c:usersu00documentsaliases.pdf" pdf
get-alias | ./out-report.ps1 "c:usersu00documentsaliases.jpeg" -filetype image -imagetype JPEG -height 22 -width 11
 
The script makes use of a DataTable routine by Marc van Orsouw (//o//) . I often use variations of the DataTable code in many of my scripts, especially for importing data into SQL Server tables.
 
Andriy points out, not only can you generate Excel, PDF and images from the ReportViewer control, but you can also extended SSRS to support DOC, RTF, WordprocessingML, and OOXML. A little hacky, but interesting.
 
About these ads
This entry was posted in PowerShell. Bookmark the permalink.

One Response to Send Powershell output to an Excel, PDF, or Image file

  1. John says:

    Excellent

Comments are closed.