如何通过powershell将多个值参数传递给报告服务报告 [英] How to pass multiple value parameter to reporting services report via powershell

查看:35
本文介绍了如何通过powershell将多个值参数传递给报告服务报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以毫无问题地将单值参数传递给报告服务.但是我无法弄清楚如何将多值字符串参数发送到报告.在我的报告中,名为multival"的字段设置为允许多个值".文档 声明传入字符串[] 用于多个值.我只是无法在 PowerShell 中获得为此工作的语法.下面是一些示例代码.

I can pass single value parameters to reporting services without any problems. However I cannot figure out how to send a multiple value string parameter to the report. In my report a field named "multival" is set to "Allow multiple values". The documentation states to pass in string[] for multiple values. I just cannot get the syntax to work for this in PowerShell. Some example code is below.

$multiVal = @('item1','item2','item5','item7');

....

$params[0] = new-Object Microsoft.Reporting.WinForms.ReportParameter("multival", $multiVal, $false);

我花了很多时间在互联网上搜索,但找不到任何有关如何完成此操作的 PowerShell 特定示例.非常感谢任何帮助.

I've spent quite a bit of time searching the internet and cannot find any PowerShell specific examples of how to accomplish this. Any help is greatly appreciated.

更新 #1

以下是更完整的示例和错误消息的完整输出.

Below is a more complete sample and the full output of the error message.

# 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.一旦我这样做了,代码就起作用了.请参阅下面的更新片段.

As pointed out in the comments I needed to declare $multival as a StringCollection. Once I did that the code worked. See updated snippet below.

$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].请参阅 this 文章是如何在 PowerShell 中使用 [StringCollection] 对象的示例

According to MSDN the ReportParameter.Values property is of type [StringCollection]. PowerShell can't easily cast the [String[]] into a [StringCollection]. See this article for an example of how to use [StringCollection] objects within PowerShell

这篇关于如何通过powershell将多个值参数传递给报告服务报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆