Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get detailed exception stacktrace in PowerShell?

Runing such script:

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

I see

test At C:\test.ps1:8 char:10 

Can I get a detailed stack trace instead?

At bar() in C:\test.ps1:8 At foo() in C:\test.ps1:3  At C:\test.ps1:11 
like image 323
alex2k8 Avatar asked Apr 28 '09 00:04

alex2k8


People also ask

How do I get exception details in PowerShell?

GetType(). FullName to view the exception message for the last error that occurred. Going back to the PowerShell console, rerun the New-Item command with a non-existent path, then run the $Error command to find the exception message.

What is exception StackTrace?

A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.

How do I get an error message in PowerShell?

You can use Get-Error to display a specified number of errors that have occurred in the current session using the Newest parameter. The Get-Error cmdlet also receives error objects from a collection, such as $Error , to display multiple errors from the current session.


1 Answers

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    } } 
like image 200
Andy Schneider Avatar answered Sep 21 '22 10:09

Andy Schneider