Get-Error

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

Syntax

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

Description

Cmdlet Get-Error 會取得 PSExtendedError 物件,此物件代表會話中最後一個錯誤發生的目前錯誤詳細數據。

您可以使用 Get-Error 顯示目前工作階段 中使用 Newest 參數所發生的指定錯誤數目。

Cmdlet Get-Error 也會從集合接收錯誤物件,例如 $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:取得目前會話中發生的指定錯誤訊息數目

此範例示範如何搭配 Newest 參數使用Get-Error。 在此範例中, 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