如何从ForEach循环内部调用的函数中获取PowerShell错误详细信息

如何从ForEach循环内部调用的函数中获取PowerShell错误详细信息,powershell,Powershell,此代码按预期工作: function Foo ($Dividend) { $Divisor = 0 $Quotient = $Dividend / $Divisor } try { Foo 1 } catch { $Line = $($_.InvocationInfo.Line).Trim() $Row = $($_.InvocationInfo.ScriptLineNumber) $Col = $($_.InvocationInfo.Offse

此代码按预期工作:

function Foo ($Dividend) {
    $Divisor = 0
    $Quotient = $Dividend / $Divisor
}

try {
    Foo 1
} catch {
    $Line = $($_.InvocationInfo.Line).Trim()
    $Row = $($_.InvocationInfo.ScriptLineNumber)
    $Col = $($_.InvocationInfo.OffsetInLine)

    Write-Host "Error: $_" -ForegroundColor "Red"
    Write-Host "  -> Row:    $Row" -ForegroundColor "Red"
    Write-Host "  -> Col:    $Col" -ForegroundColor "Red"
    Write-Host "  -> Line:   $Line" -ForegroundColor "Red"
}
它打印以下文本:

Error: Attempted to divide by zero. -> Row: 3 -> Col: 5 -> Line: $Quotient = $Dividend / $Divisor 它打印这个:

Error: Attempted to divide by zero. -> Row: 11 -> Col: 22 -> Line: $Dividends | ForEach { 错误:试图除以零。 ->第11排 ->上校:22 ->行:$股息|外汇{
当从
ForEach
循环中调用函数时,是否有方法获取范围正确的错误信息?

您要查找的信息嵌套在顶级
ErrorRecord
对象中。您需要展开错误对象以获取嵌套信息

更改此项:

} catch {
    $Line = $($_.InvocationInfo.Line).Trim()
    $Row = $($_.InvocationInfo.ScriptLineNumber)
    $Col = $($_.InvocationInfo.OffsetInLine)

    Write-Host "Error: $_" -ForegroundColor "Red"
    Write-Host "  -> Row:    $Row" -ForegroundColor "Red"
    Write-Host "  -> Col:    $Col" -ForegroundColor "Red"
    Write-Host "  -> Line:   $Line" -ForegroundColor "Red"
}
为此:

} catch {
    $Line = $($_.Exception.ErrorRecord.InvocationInfo.Line).Trim()
    $Row = $($_.Exception.ErrorRecord.InvocationInfo.ScriptLineNumber)
    $Col = $($_.Exception.ErrorRecord.InvocationInfo.OffsetInLine)

    Write-Host "Error: $_" -ForegroundColor "Red"
    Write-Host "  -> Row:    $Row" -ForegroundColor "Red"
    Write-Host "  -> Col:    $Col" -ForegroundColor "Red"
    Write-Host "  -> Line:   $Line" -ForegroundColor "Red"
}
}catch{
$Line=$($\.Exception.ErrorRecord.InvocationInfo.Line).Trim()
$Row=$($\.Exception.ErrorRecord.InvocationInfo.ScriptLineNumber)
$Col=$($\异常.ErrorRecord.InvocationInfo.OffsetLine)
写入主机“错误:$\”-ForegroundColor“红色”
写入主机“->行:$Row”-ForegroundColor“红色”
写入主机“->Col:$Col”-ForegroundColor“红色”
写入主机“->行:$Line”-ForegroundColor“红色”
}

代码的行为应该与您期望的一样。

您要查找的信息嵌套在顶级
ErrorRecord
对象中。您需要展开错误对象以获取嵌套信息

更改此项:

} catch {
    $Line = $($_.InvocationInfo.Line).Trim()
    $Row = $($_.InvocationInfo.ScriptLineNumber)
    $Col = $($_.InvocationInfo.OffsetInLine)

    Write-Host "Error: $_" -ForegroundColor "Red"
    Write-Host "  -> Row:    $Row" -ForegroundColor "Red"
    Write-Host "  -> Col:    $Col" -ForegroundColor "Red"
    Write-Host "  -> Line:   $Line" -ForegroundColor "Red"
}
为此:

} catch {
    $Line = $($_.Exception.ErrorRecord.InvocationInfo.Line).Trim()
    $Row = $($_.Exception.ErrorRecord.InvocationInfo.ScriptLineNumber)
    $Col = $($_.Exception.ErrorRecord.InvocationInfo.OffsetInLine)

    Write-Host "Error: $_" -ForegroundColor "Red"
    Write-Host "  -> Row:    $Row" -ForegroundColor "Red"
    Write-Host "  -> Col:    $Col" -ForegroundColor "Red"
    Write-Host "  -> Line:   $Line" -ForegroundColor "Red"
}
}catch{
$Line=$($\.Exception.ErrorRecord.InvocationInfo.Line).Trim()
$Row=$($\.Exception.ErrorRecord.InvocationInfo.ScriptLineNumber)
$Col=$($\异常.ErrorRecord.InvocationInfo.OffsetLine)
写入主机“错误:$\”-ForegroundColor“红色”
写入主机“->行:$Row”-ForegroundColor“红色”
写入主机“->Col:$Col”-ForegroundColor“红色”
写入主机“->行:$Line”-ForegroundColor“红色”
}

代码的行为应该与您期望的一样。

我非常确定您需要将try/catch放在foreach块中,就像@Eris:一样,也可以,谢谢。我非常确定您需要将try/catch放在foreach块中,就像@Eris:一样,也可以,谢谢。