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

标签 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( $_ )
    }

间歇性地,我们的构建将失败并显示以下错误:

Cannot find an overload for "Add" and the argument count: "1".
At line:XXXXX char: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

最佳答案

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

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

关于powershell - 为什么 PowerShell 间歇性找不到 List[string].Add() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32261033/

相关文章:

azure - 如何在 Azure PowerShell 中隐藏错误消息中的敏感值?

powershell - 创建自签名证书-使用Powershell的./makecert

powershell - 如何使用单个命令加载自定义 powershell 配置文件

powershell - 使用PoSH取值,奇怪的结果

powershell - 获取 USB 端口名称和详细信息

c# - 在 C# 中检查 PowerShell 执行策略的最佳方法是什么?

c# - 在 Powershell 中,如何使用字节数组作为 C# 通用类之一的键类型?

Powershell where-object 语法 - 属性中的连字符

regex - 如何用逗号分割字符串忽略双引​​号中的逗号

c# - Powershell 未在 try/catch 中捕获异常?