为什么PowerShell间歇性地找不到List[string].Add()方法?
我有一个构建脚本(Windows 2012 R2上的PowerShell 4),它在后台作业中运行NUnit并返回NUnit的输出。此输出收集在为什么PowerShell间歇性地找不到List[string].Add()方法?,powershell,Powershell,我有一个构建脚本(Windows 2012 R2上的PowerShell 4),它在后台作业中运行NUnit并返回NUnit的输出。此输出收集在Collections.Generic.List[string]中 $nunitJob = Start-Job -ScriptBlock { param( [string]
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”和参数计数的重载:“1”。第XXXXX行字符:XXXXX
+[void]$output.Add($\ux)
+~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:NotSpecified:(:)[],MethodException
+FullyQualifiedErrorId:MethodCountNotFindBest 知道为什么PowerShell无法找到
列表[string]
的添加方法吗?
我打开了一个控制台窗口,将不同类型的对象传递给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是字符串
因此,您可能希望查看输出是否包含感兴趣的错误记录,如果不包含,则将其过滤掉。问题很可能是它无法隐式地将
$\ucode>转换为字符串。像$o.Add($(新对象psobject))
@zespri我确实删除了一些行,但是没有涉及到$output
@MathiasR.Jessen,可能就是这样。我尝试了$output.Add([pscustomobject]@{})
,并且能够重现错误。现在我必须弄清楚为什么PowerShell不将控制台输出视为字符串。“将控制台输出视为字符串”?在foreach对象期间,您是否尝试以任何方式修改$\uuu
?同样,整个代码块在这里可能是有用的/有意义的。可能值得添加一个Write Verbose$\.GetType()。