How to get debug diagnostic information from Powershell commands

There are occasions where someone will use Powershell and type in a command and the command will fail with an error and not display anything back to you. Being that Powershell has been written in the .net framework we can capture error conditions by using the System.Exception Class object. The System.Exception class represents errors that occur during application execution.

If you suspect that your Powershell commands are failing with an exception, all you need to type the commands below in the order listed below:

Example:

1. Run your Powershell command: Get-PublicFolder
2. Set your parameter to capture your error:

$e = $error[0]
$e.Exception.StackTrace
$e.Exception.InnerException
$e.Exception.InnerException.StackTrace

Here is what you can except to see:

[PS] c:\>Get-PublicFolder
[PS] c:\>$e.Exception.InnerException
MapiExceptionUnknownUser: Unable to make connection to the server. (hr=0x80004005, ec=1003)

Diagnostic context:
Lid: 23065 EcDoConnectEx called [length=160]
Lid: 17913 EcDoConnectEx returned [ec=0x3EB][length=56][latency=0]
Lid: 19778
Lid: 27970 StoreEc: 0x3EB
Lid: 17730
Lid: 25922 StoreEc: 0x3EB

Using the err.exe (Microsoft Exchange Server Error Code Look-up Tool) you canĀ  look up both error codes:

C:\err.exe 0x80004005
# for hex 0x80004005 / decimal -2147467259
ecError ec.h
MAPI_E_CALL_FAILED mapicode.h
STIERR_GENERIC stierr.h
E_FAIL winerror.h
# Unspecified error

Dave