共用方式為


Get-Error

取得並顯示目前會話的最新錯誤訊息。

語法

Newest (預設值)

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

Error

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

Description

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

此 Cmdlet 會傳回 PSExtendedError 物件。

備註

PowerShell 包含下列 Get-Error的別名:

  • 所有平臺:
    • gerr

Get-Error 接受管線輸入。 例如: $Error | Get-Error