February 2019
Intermediate to advanced
626 pages
15h 51m
English
When an error is raised in PowerShell, an ErrorRecord object is created (explicitly or implicitly).
An ErrorRecord object contains a number of fields that are useful for diagnosing an error. ErrorRecord can be explored using Get-Member. For example, an ErrorRecord will be generated when attempting to divide by 0:
100 / 0 $record = $Error[0]
The ErrorRecord object that was generated includes ScriptStackTrace. ScriptTrackTrace is extremely useful when debugging problems in larger scripts:
PS> $record.ScriptStackTraceat <ScriptBlock>, <No file>: line 1
The Exception in the error record also includes a .NET stack trace:
PS> $record.Exception.StackTraceat System.Management.Automation.IntOps.Divide(Int32 lhs, Int32 rhs)at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite ...
Read now
Unlock full access