How to pass multiple value parameter to reporting

2019-09-18 08:56发布

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);

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.

UPDATE #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();
}

Error is as follows:

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

UPDATE #2

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')

1条回答
Animai°情兽
2楼-- · 2019-09-18 09:20

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

查看更多
登录 后发表回答