Get-Error

获取并显示当前会话中的最新错误消息。

语法

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

说明

Get-Error cmdlet 获取 PSExtendedError 对象,该对象表示会话中发生的最后一个错误的当前错误详细信息。

可以使用 Get-Error 显示当前会话中使用 Newest 参数发生的指定数量的错误。

Get-Error cmdlet 还会从集合(如 $Error)接收错误对象,以显示当前会话中的多个错误。

示例

示例 1:获取最新的错误详细信息

在此示例中,Get-Error 显示当前会话中发生的最新错误的详细信息。

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 :

示例 2:获取当前会话中发生的指定错误消息数

此示例演示如何将 Get-ErrorNewest 参数一起使用。 在此示例中,Newest 返回此会话中发生的 3 个最新错误的详细信息。

Get-Error -Newest 3

示例 3:发送错误集合以接收详细消息

$Error 自动变量包含当前会话中的错误对象数组。 可以通过管道将对象数组传递给 Get-Error 以接收详细的错误消息。

在此示例中,$Error 通过管道传递给 Get-Error cmdlet。 结果是详细的错误消息列表,类似于示例 1 的结果。

$Error | Get-Error

参数

-InputObject

此参数用于管道输入。

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

-Newest

指定要在当前会话中显示的已发生错误数。

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

输入

PSObject

可以将任何 PSObject 传递给此 cmdlet,但结果会有所不同,除非提供了 ErrorRecordException 对象。

输出

ErrorRecord

此 cmdlet 返回 PSExtendedError 对象。

备注

PowerShell 包含 Get-Error 的以下别名:

  • 所有平台:
    • gerr

Get-Error 接受管道输入。 例如 $Error | Get-Error