Επεξεργασία

Κοινή χρήση μέσω


Get-Error

Gets and displays the most recent error messages from the current session.

Syntax

Get-Error
   [[-Newest] <Int32>]
   [<CommonParameters>]
Get-Error
   [-InputObject <PSObject>]
   [<CommonParameters>]

Description

The Get-Error cmdlet gets a PSExtendedError object that represents the current error details from the last error that occurred in the session.

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.

Examples

Example 1: Get the most recent error details

In this example, Get-Error displays the details of the most recent error that occurred in the current session.

Get-Childitem -path /NoRealDirectory
Get-Error

Get-ChildItem: Cannot find path 'C:\NoRealDirectory' because it does not exist.

Exception             :
    ErrorRecord          :
        Exception             :
            Message : Cannot find path 'C:\NoRealDirectory' because it does not exist.
            HResult : -2146233087
        TargetObject          : C:\NoRealDirectory
        CategoryInfo          : ObjectNotFound: (C:\NoRealDirectory:String) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : PathNotFound
    ItemName             : C:\NoRealDirectory
    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:\NoRealDirectory' because it does not exist.
    Source               : System.Management.Automation
    HResult              : -2146233087
TargetObject          : C:\NoRealDirectory
CategoryInfo          : ObjectNotFound: (C:\NoRealDirectory:String) [Get-ChildItem], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
InvocationInfo        :
    MyCommand        : Get-ChildItem
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 57
    Line             : Get-Childitem -path c:\NoRealDirectory
    PositionMessage  : At line:1 char:1
                       + Get-Childitem -path c:\NoRealDirectory
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Get-Childitem
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :

Example 2: Get the specified number of error messages that occurred in the current session

This example shows how to use Get-Error with the Newest parameter. In this example, Newest returns the details of the 3 newest errors that occurred in this session.

Get-Error -Newest 3

Example 3: Send a collection of errors to receive detailed messages

The $Error automatic variable contains an array of error objects in the current session. The array of objects can be piped to Get-Error to receive detailed error messages.

In this example, $Error is piped to the Get-Error cmdlet. the result is list of detailed error messages, similar to the result of Example 1.

$Error | Get-Error

Parameters

-InputObject

This parameter is used for pipeline input.

Type:PSObject
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Newest

Specifies the number of errors to display that have occurred in the current session.

Type:Int32
Aliases:Last
Position:0
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

Inputs

PSObject

You can pipe any PSObject to this cmdlet, but results vary unless either an ErrorRecord or Exception object is supplied.

Outputs

ErrorRecord

This cmdlet returns a PSExtendedError object.

Notes

PowerShell includes the following aliases for Get-Error:

  • All platforms:
    • gerr

Get-Error accepts pipeline input. For example, $Error | Get-Error.