Monthly Archive

Categories

Get-Error

One of the experimental features new PowerShell v7 preview 5 is the Get-Error cmdlet. The features description states:

Enable Get-Error cmdlet that displays detailed information about ErrorRecords included nested objects

 

Enable the feature:

PS> Enable-ExperimentalFeature -Name Microsoft.PowerShell.Utility.PSGetError
WARNING: Enabling and disabling experimental features do not take effect until next start of PowerShell.

 

It would be better for users if the naming of experimental features became more consistent and ideally a single word rather than a fully qualified name including the module as this and some other new experimental features have.

 

The cmdlet has a simple syntax:

PS> Get-Command Get-Error -Syntax

Get-Error [-Newest <int>] [<CommonParameters>]

Get-Error [[-InputObject] <psobject>] [<CommonParameters>]

 

Generate some errors:

PS> 1/ 0
RuntimeException: Attempted to divide by zero.
PS> Get-ChildItem -Path c:\nosuchfile
Get-ChildItem: Cannot find path 'C:\nosuchfile' because it does not exist.

 

Using Get-Error gives a very comprehensive view of the errors

PS> Get-Error

Exception :
ErrorRecord :
Exception :
Message : Cannot find path 'C:\nosuchfile' because it does not exist.
HResult : -2146233087
TargetObject : C:\nosuchfile
CategoryInfo : ObjectNotFound: (C:\nosuchfile:String) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : PathNotFound
ItemName : C:\nosuchfile
SessionStateCategory : Drive
TargetSite :
Name : GetChildItems
DeclaringType : System.Management.Automation.SessionStateInternal
MemberType : Method
Module : System.Management.Automation.dll
StackTrace :
at System.Management.Automation.SessionStateInternal.GetChildItems(String path, Boolean recurse, UInt32 depth,
CmdletProviderContext context)
at System.Management.Automation.ChildItemCmdletProviderIntrinsics.Get(String path, Boolean recurse, UInt32 depth,
CmdletProviderContext context)
at Microsoft.PowerShell.Commands.GetChildItemCommand.ProcessRecord()
Message : Cannot find path 'C:\nosuchfile' because it does not exist.
Source : System.Management.Automation
HResult : -2146233087
TargetObject : C:\nosuchfile
CategoryInfo : ObjectNotFound: (C:\nosuchfile:String) [Get-ChildItem], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
InvocationInfo :
MyCommand : Get-ChildItem
ScriptLineNumber : 1
OffsetInLine : 2
HistoryId : 5
Line : Get-ChildItem -Path c:\nosuchfile
PositionMessage : At line:1 char:2
+ Get-ChildItem -Path c:\nosuchfile
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
InvocationName : Get-ChildItem
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :

 

This is the latest error in the error collection. You’ll see the same information if you use

PS> $error[0]

 

If you use the Newest parameter you’ll also see the Error Index

PS> Get-Error -Newest 2

ErrorIndex: 0

Exception :
ErrorRecord :
Exception :
Message : Cannot find path 'C:\nosuchfile' because it does not exist.
HResult : -2146233087
TargetObject : C:\nosuchfile
CategoryInfo : ObjectNotFound: (C:\nosuchfile:String) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : PathNotFound
ItemName : C:\nosuchfile
SessionStateCategory : Drive
TargetSite :
Name : GetChildItems
DeclaringType : System.Management.Automation.SessionStateInternal
MemberType : Method
Module : System.Management.Automation.dll
StackTrace :
at System.Management.Automation.SessionStateInternal.GetChildItems(String path, Boolean recurse, UInt32 depth,
CmdletProviderContext context)
at System.Management.Automation.ChildItemCmdletProviderIntrinsics.Get(String path, Boolean recurse, UInt32 depth,
CmdletProviderContext context)
at Microsoft.PowerShell.Commands.GetChildItemCommand.ProcessRecord()
Message : Cannot find path 'C:\nosuchfile' because it does not exist.
Source : System.Management.Automation
HResult : -2146233087
TargetObject : C:\nosuchfile
CategoryInfo : ObjectNotFound: (C:\nosuchfile:String) [Get-ChildItem], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
InvocationInfo :
MyCommand : Get-ChildItem
ScriptLineNumber : 1
OffsetInLine : 2
HistoryId : 5
Line : Get-ChildItem -Path c:\nosuchfile
PositionMessage : At line:1 char:2
+ Get-ChildItem -Path c:\nosuchfile
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
InvocationName : Get-ChildItem
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :

ErrorIndex: 1

Exception :
ErrorRecord :
Exception :
Message : Attempted to divide by zero.
HResult : -2146233087
CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : RuntimeException
InvocationInfo :
ScriptLineNumber : 1
OffsetInLine : 2
HistoryId : -1
Line : 1/ 0
PositionMessage : At line:1 char:2
+ 1/ 0
+ ~~~~
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
TargetSite :
Name : Divide
DeclaringType : System.Management.Automation.IntOps
MemberType : Method
Module : System.Management.Automation.dll
StackTrace :
at System.Management.Automation.IntOps.Divide(Int32 lhs, Int32 rhs)
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Message : Attempted to divide by zero.
Data : System.Collections.ListDictionaryInternal
InnerException :
Message : Attempted to divide by zero.
HResult : -2147352558
Source : System.Management.Automation
HResult : -2146233087
CategoryInfo : NotSpecified: (:) [], RuntimeException
FullyQualifiedErrorId : RuntimeException
InvocationInfo :
ScriptLineNumber : 1
OffsetInLine : 2
HistoryId : -1
Line : 1/ 0
PositionMessage : At line:1 char:2
+ 1/ 0
+ ~~~~
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1

 

This seems to give a concise view of the Index and the error

PS> Get-Error -Newest 2 | Select PSErrorIndex, @{N='Error'; E={$_}} | Format-List

PSErrorIndex : 0
Error : Cannot find path 'C:\nosuchfile' because it does not exist.

PSErrorIndex : 1
Error : Attempted to divide by zero.

 

I think I’d rather see Get-Error by itself supply a list of current errors and their index. There should also be an Index parameter that enables you to pick a specific error. Otherwise this looks like a useful addition.

Comments are closed.