当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Get-Error (Microsoft.PowerShell.Utility)

作者:精品下载站 日期:2024-12-14 02:07:11 浏览:13 分类:玩电脑

Get-Error (Microsoft.PowerShell.Utility)


Get-Error

模块 :Microsoft.PowerShell.Utility

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

句法

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

Get-Error -Newest 3

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

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

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

$Error | Get-Error

参数

-InputObject

该参数用于管道输入。

类型 :

PS对象

位置:

命名

默认值:

None

必需的:

False

接受管道输入:

True

接受通配符:

False

-Newest

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

类型 :

整数32

别名:

Last

位置:

0

默认值:

None

必需的:

False

接受管道输入:

False

接受通配符:

False

输入

PSObject

您可以将任何 PSObject 通过管道传输到此 cmdlet,但结果会有所不同,除非提供 ErrorRecordException 对象。

输出

错误记录

此 cmdlet 返回一个 PSExtendedError 对象。

笔记

PowerShell 包含以下 Get-Error 别名:

  • 所有平台:

      gerr

    Get-Error 接受管道输入。例如,$Error |获取错误

    您需要 登录账户 后才能发表评论

    取消回复欢迎 发表评论:

    关灯