使用 PowerShell 启动作业的 SSRS 报告

问题描述

我有一个 PowerShell GUI,它使用字符串数组输入从 SSRS 报告中提取一些值。但是,由于这会冻结 GUI,因此我决定使用 Start-Job 来启动一项工作,该工作在 ProgressBar 继续在 GUI 中运行时提取 SSRS 报告。

SSRS 报告只有一个输入参数。当我使用 Start-Job 使用多个值呈现报告时,无论输入值的数量如何,我都只会得到第一条记录的结果。

在不返回所有输入值的记录的情况下本地调用时,相同的函数可以顺利运行。

这是代码

Start-Job

当我如下所示更改底部时,我能够验证在输出第一条记录后作业是否结束。

$GetSSRSData = {
    param([string[]]$InputArray)

    $reportserverURI = "https://<SERVER>/reportserver/ReportExecution2005.asmx?wsdl"
    $RS = New-WebServiceProxy -Class 'RS' -NameSpace 'RS' -Uri $reportserverURI -UseDefaultCredential
    $RS.Url = $reportserverURI

    $deviceInfo = "<DeviceInfo><NoHeader>True</NoHeader></DeviceInfo>"
    $extension = ""
    $mimeType = ""
    $encoding = ""
    $warnings = $null
    $streamIDs = $null

    $reportPath = "/Folder/Report"
    $Report = $RS.GetType().getmethod("LoadReport").Invoke($RS,@($reportPath,$null))

    # Report parameters are handled by creating an array of ParameterValue objects.
    $parameters = @()

    for($i = 0; $i -lt $InputArray.Count; $i++){
        $parameters += New-Object RS.ParameterValue
        $parameters[$i].Name  = "ParameterName"
        $parameters[$i].Value = "$($InputArray[$i])"
    }

    # Add the parameter array to the service.  Note that this returns some
    # @R_269_4045@ion 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.
    $renderOutput = $RS.Render('CSV',$deviceInfo,[ref] $extension,[ref] $mimeType,[ref] $encoding,[ref] $warnings,[ref] $streamIDs
    )

    $output = [System.Text.Encoding]::ASCII.GetString($renderOutput)
    return $output
}
$InputArray = @('XXXXXX','YYYYYY','ZZZZZZ','ABCDEF')

<#
# The below code works perfectly
$Data = GetSSRSData -InputArray $InputArray
ConvertFrom-Csv -InputObject $Data
#>

$job = Start-Job -ScriptBlock $GetSSRSData -ArgumentList $InputArray
do { [System.Windows.Forms.Application]::DoEvents() } until ($job.State -ne "Running")
$Data = Receive-Job -Job $job
Write-Host $Data # returns only the first record

我也试过在Render函数添加5秒的睡眠函数,但没有任何区别。

请注意,不能为每个输入重复调用 Start-Job,因为每个函数调用都会花费大量时间,因此需要在一次调用提取报告。

  • 为什么 Render 函数在作为作业启动时表现不同?函数是否在呈现其他记录之前提前结束?
  • 是否有其他方法(例如 Runspace 或 Start-ThreadJob)可以解决此问题?

参考:https://stackoverflow.com/a/63253699/4137016

解决方法

答案在这里:ArgumentList parameter in Invoke-Command don't send all array
这里可能有更好的答案:How do I pass an array as a parameter to another script?

-ArgumentList $InputArray 改为 -ArgumentList (,$InputArray)

$InputArray = @('XXXXXX','YYYYYY','ZZZZZZ','ABCDEF')
$job = Start-Job -ScriptBlock $GetSSRSData -ArgumentList (,$InputArray)