[英]How to pass multiple value parameter to reporting services report via powershell
我可以毫无问题地将单值参数传递给报表服务。 但是,我无法弄清楚如何向报表发送多值字符串参数。 在我的报告中,名为“ multival”的字段设置为“允许多个值”。 文档指出要在string []中传递多个值。 我只是无法获得在PowerShell中为此工作的语法。 下面是一些示例代码。
$multiVal = @('item1','item2','item5','item7');
....
$params[0] = new-Object Microsoft.Reporting.WinForms.ReportParameter("multival", $multiVal, $false);
我花了很多时间在互联网上搜索,但是找不到有关如何完成此操作的PowerShell特定示例。 任何帮助是极大的赞赏。
更新#1
下面是一个更完整的示例以及错误消息的完整输出。
# Function to save each report as a PDF file
function Generate-Report
{
Param ($param1, $param2, $param3, $startdate, $enddate)
# Create a report viewer
$rv = New-Object Microsoft.Reporting.WinForms.ReportViewer;
$rv.ServerReport.ReportServerUrl = "http://myserver/reportserver";
$rv.ServerReport.ReportPath = "/Folder/Folder/Report";
$rv.ProcessingMode = [Microsoft.Reporting.WinForms.ProcessingMode]::Remote;
$rv.ShowParameterPrompts = $false;
# Need these variables for PDF rendering
$deviceInfo = $null;
$mimeType = $null;
$encoding = $null;
$extension = $null;
$streamids = $null;
$warnings = $null;
$multival = @('item1','item2','item4','item5','item8','item10'); # This does not work
#$multival = @('item1'); # This works
# Set Parameters
$params = $null;
$params = New-Object 'Microsoft.Reporting.WinForms.ReportParameter[]' 5
$params[0] = New-Object Microsoft.Reporting.WinForms.ReportParameter("startdate", $startdate, $false);
$params[1] = New-Object Microsoft.Reporting.WinForms.ReportParameter("enddate", $enddate, $false);
$params[2] = New-Object Microsoft.Reporting.WinForms.ReportParameter("param1", $param1, $false);
$params[3] = New-Object Microsoft.Reporting.WinForms.ReportParameter("param3", $param3, $false);
$params[4] = New-Object Microsoft.Reporting.WinForms.ReportParameter("multival", $multival, $false);
$rv.ServerReport.SetParameters($params);
$rv.ServerReport.Refresh();
# Render the report as PDF
$bytes = $null;
$bytes = $rv.ServerReport.Render("PDF", $deviceInfo,
[ref] $mimeType,
[ref] $encoding,
[ref] $extension,
[ref] $streamids,
[ref] $warnings);
# Save the PDF file to the designated location
$file = "$outputFolder\$param1 - $param2.pdf";
$fileStream = New-Object System.IO.FileStream($file, [IO.FileMode]::OpenOrCreate, [IO.FileAccess]::Write, [IO.FileShare]::None)
$fileStream.Write($bytes, 0, $bytes.Length);
$fileStream.Close();
}
错误如下:
Exception calling "Render" with "7" argument(s): "This report requires a default or
user-define value for the report parameter 'multival'. To run or subscribe to this
report, you must provide a parameter value. (rsReportParameterValueNotSet)"
At C:\MyScript.ps1:85 char:37
+ $bytes = $rv.ServerReport.Render <<<< ("PDF", $deviceInfo,
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Write" with "3" argument(s): "Buffer cannot be null. Parameter
name: array"
At C:\MyScript.ps1:95 char:22
+ $fileStream.Write <<<< ($bytes, 0, $bytes.Length);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
更新#2
正如评论中指出的那样,我需要将$ multival声明为StringCollection。 一旦我做到了,代码就可以工作了。 请参阅下面的更新的代码段。
$multival = New-Object System.Collections.Specialized.StringCollection
$ret = $multival.Add('item1')
$ret = $multival.Add('item2')
$ret = $multival.Add('item4')
$ret = $multival.Add('item5')
$ret = $multival.Add('item8')
$ret = $multival.Add('item10')
根据MSDN,ReportParameter.Values属性的类型为[StringCollection]。 PowerShell无法轻松将[String []]强制转换为[StringCollection]。 请参见本文章如何使用[StringCollection]的例子在PowerShell对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.