0
我可以將單值參數傳遞給報告服務,而不會出現任何問題。但是我不知道如何發送一個多值字符串參數給報告。在我的報告中,名爲「多值」的字段設置爲「允許多個值」。 documentation聲明爲多個值傳遞字符串[]。我只是無法在PowerShell中獲得適用於此的語法。下面是一些示例代碼。如何通過powershell將多個值參數傳遞給報告服務報告
$multiVal = @('item1','item2','item5','item7');
....
$params[0] = new-Object Microsoft.Reporting.WinForms.ReportParameter("multival", $multiVal, $false);
我花了相當多的時間來搜索互聯網,找不到如何做到這一點的任何PowerShell的具體例子。任何幫助是極大的讚賞。
UPDATE#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')
string []僅表示一個字符串數組。只要傳遞這樣的東西:('item','item2')。其實你這樣做 - 我不明白爲什麼你的例子不起作用。 – Swonkie 2014-11-14 20:48:25
@Swonkie - 是的,我同意並嘗試過,但它只是不喜歡它。如果我通過('item1'),沒有問題的工作。然而('item1','item2')不起作用,我在「多值」上得到rsReportParameterValueNotSet。 - 剛剛看到你編輯的評論。感謝您的嘗試。 – tjpeters 2014-11-14 20:56:18
好吧,這對我有用:New-Object Microsoft.Reporting.WinForms.ReportParameter(「multival」,('foo','bar'),$ false)這不是命令,其他的東西一定是錯誤的。 – Swonkie 2014-11-14 21:01:29