5

我想做这样的事情......

try  
{  
    # Something in this function throws an exception
    Backup-Server ...  
}catch  
{  
    # Capture stack trace of where the error was thrown from
    Log-Error $error 
}

理想情况下,我想捕获函数和行号等的参数(就像您在 get-pscallstack 中看到的那样)
编辑:澄清一下,这是我想要的 powershell 堆栈跟踪,而不是 .NET
任何想法如何实现这一点?
戴夫

4

2 回答 2

8

最后一个错误位于:

$error[0]

那里有很多好信息供您追踪,包括异常堆栈跟踪。这是一个方便的小脚本(PSCX 附带的 Resolve-ErrorRecord),它显示了关于最后一个错误的大量有用信息:

param(
    [Parameter(Position=0, ValueFromPipeline=$true)]
    [ValidateNotNull()]
    [System.Management.Automation.ErrorRecord[]]
    $ErrorRecord
)
process {

        if (!$ErrorRecord)
        {
            if ($global:Error.Count -eq 0)
            {
                Write-Host "The `$Error collection is empty."
                return
            }
            else
            {
                $ErrorRecord = @($global:Error[0])
            }
        }
        foreach ($record in $ErrorRecord)
        {
            $record | Format-List * -Force
            $record.InvocationInfo | Format-List *
            $Exception = $record.Exception
            for ($i = 0; $Exception; $i++, ($Exception = $Exception.InnerException))
            {
                "$i" * 80
               $Exception | Format-List * -Force
            }
        }

}
于 2010-03-11T21:40:03.183 回答
2

您不需要像 Keith 的回答那样多的代码。

$error[0].ErrorRecord.ScriptStackTrace

是你想要的。

于 2014-12-25T16:35:03.110 回答