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

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一个结果 任何想法如何实现这一目标?结果 戴夫

有帮助吗?

解决方案

最后的错误是坐在:

$error[0]

在那里等你追下去包括异常堆栈跟踪的好消息很多。这是一个handly小脚本(解决同ErrorRecord附带PSCX),显示大量的有关最后一个错误好消息:

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
            }
        }

}

其他提示

您不需要的代码量Keith的答案。

$error[0].ErrorRecord.ScriptStackTrace

是你想要的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top