Get-Error

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

语法

Newest (默认值)

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

Error

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-Error最新 参数一起使用。 在此示例中,Newest 返回此会话中发生的 3 个最新错误的详细信息。

Get-Error -Newest 3

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

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

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

$Error | Get-Error

参数

-InputObject

此参数用于管道输入。

参数属性

类型:PSObject
默认值:None
支持通配符:False
不显示:False

参数集

Error
Position:Named
必需:False
来自管道的值:True
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Newest

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

参数属性

类型:Int32
默认值:None
支持通配符:False
不显示:False
别名:最后一个

参数集

Newest
Position:0
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 有关详细信息,请参阅 about_CommonParameters

输入

PSObject

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

输出

ErrorRecord

此命令返回 PSExtendedError 对象。

备注

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

  • 所有平台:
    • gerr

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