PoshCode Logo PowerShell Code Repository

Get-CrystalReportTable (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1471"></script>download | new post

Find the tables used in a Crystal Report.

  1. function Get-CrystalReportTable()
  2. {
  3.     #Requires -Version 2
  4.     <#
  5.     .Synopsis
  6.         Examines a Crystal Report and returns the tables used
  7.     .Description
  8.         Examines a Crystal Report and returns the tables used by the main report and subreports
  9.     .Example
  10.         $reports = Dir *.rpt | Get-CrystalReportTable
  11.     .Notes
  12.         Written by Steven Murawski on 11/12/09
  13.         http://blog.usepowershell.com
  14.         Version 0.1
  15.     #>
  16.     [cmdletbinding()]
  17.     param (
  18.         [parameter(ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
  19.         [Alias('FullName')]
  20.         [string]$Path
  21.     )
  22.     begin
  23.     {
  24.         [reflection.assembly]::LoadWithPartialName('CrystalDecisions.Shared') | Out-Null
  25.         [reflection.assembly]::LoadWithPartialName('CrystalDecisions.CrystalReports.Engine') | Out-Null
  26.     }
  27.     process
  28.     {
  29.         try
  30.         {
  31.             $output = New-Object PSObject | Select-Object Name, Tables, SubReports
  32.             $output.name = Split-Path $path -Leaf
  33.            
  34.             $report = New-Object CrystalDecisions.CrystalReports.Engine.ReportDocument
  35.             $report.load($path)
  36.             $output.Tables = $report.Database.Tables | Select-Object -expand Name
  37.            
  38.            
  39.             $subreports = @($report.ReportDefinition.ReportObjects | Where-Object {$_ -is [CrystalDecisions.CrystalReports.Engine.SubreportObject]})
  40.             if ($subreports.count -gt 0 )
  41.             {
  42.                 $output.subreports = @()
  43.                 foreach ($subreport in $subreports)
  44.                 {
  45.                     $subreportdata = New-Object PSObject | Select-Object Name, Tables
  46.                     $subreportdata.name = $subreport.SubreportName
  47.                    
  48.                     $subreportobject = $report.OpenSubreport($subreport.SubreportName)
  49.                     $subreportdata.Tables = $subreportobject.Database.Tables | Select-Object -expand Name
  50.                     $output.subreports += $subreportdata
  51.                 }
  52.             }          
  53.             Write-Output $output
  54.         }
  55.         finally
  56.         {
  57.             $report.dispose()  
  58.         }
  59.     }
  60. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me