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

时间:2015-08-27 23:26:44

标签: powershell

我有一个构建脚本(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( $_ )
    }

间歇性地,我们的构建将因此错误而失败:

  

无法找到"添加"和参数计数:" 1"。
                     在行:XXXXX字符:XXXXX
                     + [void] $ output.Add($ _)
                     + ~~~~~~~~~~~~~~~~~~~~~~~~                          + CategoryInfo:NotSpecified:(:) [],MethodException
                         + FullyQualifiedErrorId:MethodCountCouldNotFindBest

任何想法为什么PowerShell无法找到List[string] Add方法?

我打开了一个控制台窗口,并将不同类型的对象传递给Add而没有出错。

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


1

1 个答案:

答案 0 :(得分:1)

当您将stderr重定向到stdout时,可以使用穿插字符串的System.Management.Automation.ErrorRecords。当stdout是字符串时,Stderr会自动转换为ErrorRecords。

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