Mat*_*hew 3 powershell reporting-services
我已经玩弄了几天而没有运气.基本上我正在尝试使用Powershell构建一个简单的库来呈现SSRS报告.我正在使用Powershell试图在以后简化开发(而不是为每个项目编写C#应用程序).大多数情况下,这将用于通过报告安排各种事情.
我有报告渲染主要在Powershell中工作.我无法弄清楚的一件事是如何在调用render方法之前为报表提供参数.我发现了大量与C#和VB相关的代码(我在其他SSRS项目中使用过),但是我无法将其转换为Powershell.
由于我对Powershell很新,我不熟悉正确的方法.这是我一直在使用的代码:
$ReportExecutionURI = "http://glitas10//ReportServer//ReportExecution2005.asmx?wsdl"
$ReportPath = "/Financial/ExpenseReportStub"
$format = "PDF"
$deviceInfo = "<DeviceInfo><NoHeader>True</NoHeader></DeviceInfo>"
$extension = ""
$mimeType = ""
$encoding = ""
$warnings = $null
$streamIDs = $null
$Reports = New-WebServiceProxy -Uri $ReportExecutionURI -UseDefaultCredential
# Load the report
$Report = $Reports.GetType().GetMethod("LoadReport").Invoke($Reports, @($ReportPath, $null))
# Render the report
$RenderOutput = $Reports.Render($format, $deviceInfo, [ref] $extension, [ref] $mimeType, [ref] $encoding, [ref] $warnings, [ref] $streamIDs)
Run Code Online (Sandbox Code Playgroud)
这显然适用于不需要参数的报表.
关于我需要做什么来实例化正确的对象和传递参数的任何想法?
以下是我最终使用的解决方案的一些信息,以防其他人需要做同样的事情.它工作得很好.
第一种方法是构建一个由Powershell脚本使用的DLL.这很好,但它会导致两个问题.首先,您的脚本必须围绕DLL进行操作.其次,此DLL绑定到特定的SSRS服务器.为了访问另一台服务器,您必须使用多个DLL.
最后,我又回到了使用网络代理.这里的关键是使用名称空间,以便您可以实例化ParameterValue对象.这是代码:
# Create a proxy to the SSRS server and give it the namespace of 'RS' to use for
# instantiating objects later. This class will also be used to create a report
# object.
$reportServerURI = "http://<SERVER>/ReportServer/ReportExecution2005.asmx?WSDL"
$RS = New-WebServiceProxy -Class 'RS' -NameSpace 'RS' -Uri $reportServerURI -UseDefaultCredential
$RS.Url = $reportServerURI
# Set up some variables to hold referenced results from Render
$deviceInfo = "<DeviceInfo><NoHeader>True</NoHeader></DeviceInfo>"
$extension = ""
$mimeType = ""
$encoding = ""
$warnings = $null
$streamIDs = $null
# Next we need to load the report. Since Powershell cannot pass a null string
# (it instead just passses ""), we have to use GetMethod / Invoke to call the
# function that returns the report object. This will load the report in the
# report server object, as well as create a report object that can be used to
# discover information about the report. It's not used in this code, but it can
# be used to discover information about what parameters are needed to execute
# the report.
$reportPath = "/PathTo/Report"
$Report = $RS.GetType().GetMethod("LoadReport").Invoke($RS, @($reportPath, $null))
# Report parameters are handled by creating an array of ParameterValue objects.
$parameters = @()
$parameters += New-Object RS.ParameterValue
$parameters[0].Name = "Parameter 1"
$parameters[0].Value = "Value"
$parameters += New-Object RS.ParameterValue
$parameters[1].Name = "Parameter 2"
$parameters[1].Value = "Value"
# Add the parameter array to the service. Note that this returns some
# information about the report that is about to be executed.
$RS.SetExecutionParameters($parameters, "en-us") > $null
# Render the report to a byte array. The first argument is the report format.
# The formats I've tested are: PDF, XML, CSV, WORD (.doc), EXCEL (.xls),
# IMAGE (.tif), MHTML (.mhtml).
$RenderOutput = $RS.Render('PDF',
$deviceInfo,
[ref] $extension,
[ref] $mimeType,
[ref] $encoding,
[ref] $warnings,
[ref] $streamIDs
)
# Convert array bytes to file and write
$Stream = New-Object System.IO.FileStream("output.pdf"), Create, Write
$Stream.Write($RenderOutput, 0, $RenderOutput.Length)
$Stream.Close()
Run Code Online (Sandbox Code Playgroud)
这似乎很容易,而且确实如此.这种方法效果非常好,是我现在用来呈现和通过电子邮件发送预定报告的方法,因为它提供了比内置SSRS调度更多的灵活性.另外,它相对较快.我用来邮寄报告的一个脚本可以每分钟渲染和发送大约20-30个报告.
| 归档时间: |
|
| 查看次数: |
14247 次 |
| 最近记录: |