Can I get detailed exception stacktrace in PowerShell?

前端 未结 12 1146
暗喜
暗喜 2020-12-02 12:23

Runing such script:

 1: function foo()
 2: {
 3:    bar
 4: }
 5: 
 6: function bar()
 7: {
 8:     throw \"test\"
 9: }
10: 
11: foo

I see

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 12:43

    There is a function up on the PowerShell Team blog called Resolve-Error which will get you all kinds of details

    Note that $error is an array of all the errors you have encountered in your PSSession. This function will give you details on the last error you encountered.

    function Resolve-Error ($ErrorRecord=$Error[0])
    {
       $ErrorRecord | Format-List * -Force
       $ErrorRecord.InvocationInfo |Format-List *
       $Exception = $ErrorRecord.Exception
       for ($i = 0; $Exception; $i++, ($Exception = $Exception.InnerException))
       {   "$i" * 80
           $Exception |Format-List * -Force
       }
    }
    

提交回复
热议问题