为什么PowerShell间歇性地找不到List[string]. add()方法?



我有一个构建脚本(Windows 2012 R2上的PowerShell 4),在后台作业中运行NUnit并返回NUnit的输出。该输出在Collections.Generic.List[string]中收集。

$nunitJob = Start-Job -ScriptBlock { 
                                    param(
                                        [string]
                                        $BinRoot,
                                        [string]
                                        $NUnitConsolePath,
                                        [string[]]
                                        $NUnitParams,
                                        [string]
                                        $Verbose
                                    )
                                    Set-Location -Path $BinRoot
                                    $VerbosePreference = $Verbose
                                    Write-Verbose -Message ('{0} {1}' -f $NUnitConsolePath,($NUnitParams -join ' '))
                                    & $NUnitConsolePath $NUnitParams 2>&1 
                                    $LASTEXITCODE
                                 } -ArgumentList $binRoot,$nunitConsolePath,$nunitParams,$VerbosePreference
$nunitJob | Wait-Job -Timeout ($timeoutMinutes * 60) | Out-Null
$jobKilled = $false
if( $nunitJob.JobStateInfo.State -eq [Management.Automation.JobState]::Running )
{
    $jobKilled = $true
    $errMsg = 'Killing {0} tests: exceeded {1} minute timeout.' -f $assembly.Name,$timeoutMinutes
    Write-Error -Message $errMsg
}
$output = New-Object 'Collections.Generic.List[string]'
$nunitJob | 
    Stop-Job -PassThru | 
    Receive-Job |
    ForEach-Object  { 
        if( -not $_ )
        {
            [void]$output.Add( '' )
            return
        }
        switch -Regex ( $_ )
        {
            '^Tests run: (d+), Errors: (d+), Failures: (d+), Inconclusive: (d+), Time: ([d.]+) seconds$'
            {
                $testsRun = $Matches[1]
                $errors = $Matches[2]
                $failures = $Matches[3]
                $inconclusive = $Matches[4]
                $duration = New-Object 'TimeSpan' 0,0,$Matches[5]
                break
            }
            '^  Not run: (d+), Invalid: (d+), Ignored: (d+), Skipped: (d+)$'
            {
                $notRun = $Matches[1]
                $invalid = $Matches[2]
                $ignored = $Matches[3]
                $skipped = $Matches[4]
                break
            }
        }
        # Error happens here:
        [void] $output.Add( $_ )
    }
间歇性地,我们的构建将失败,并出现以下错误:

找不到"Add"和实参count: "1"的重载。
字符:XXXXX
+ [void] $output。Add($_)
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo: NotSpecified: (:) [], MethodException
+ fulllyqualifiederrid: MethodCountCouldNotFindBest

知道为什么PowerShell不能找到List[string]Add方法吗?

我打开了一个控制台窗口,并在没有得到错误的情况下将不同类型的对象传递给Add

> $o = New-Object 'Collections.Generic.List[string]'
> $o.Add( '' )
> $o.Add( $null )
> $o.Add( 1 )
> $o

1

当您将标准错误重定向到标准输出时,您可以获得散布着字符串的System.Management.Automation.ErrorRecords。Stderr被自动转换为ErrorRecords,而stdout是字符串。

因此,您可能希望查看输出是否包含感兴趣的ErrorRecords,如果没有,则将它们过滤掉。

最新更新