Get-Error

現在のセッションの最新のエラー メッセージを取得して表示します。

構文

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

説明

Get-Error コマンドレットは、セッションで発生した最後のエラーの現在のエラーの詳細を表すPSExtendedError オブジェクトを取得します。

Get-Errorを使用すると、Newest パラメーターを使用して、現在のセッションで発生したエラーの数を指定して表示できます。

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 し、詳細なエラー メッセージを受け取ることができます。

この例では、 $ErrorGet-Error コマンドレットにパイプ処理されます。 結果は、例 1 の結果と同様に、詳細なエラー メッセージの一覧です。

$Error | Get-Error

パラメーター

-InputObject

このパラメーターは、パイプライン入力に使用されます。

型:PSObject
配置:Named
規定値:None
必須:False
パイプライン入力を受け取る:True
ワイルドカード文字を受け取る:False

-Newest

現在のセッションで発生したエラーの数を指定します。

型:Int32
Aliases:Last
配置:0
規定値:None
必須:False
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

入力

PSObject

任意の PSObject をこのコマンドレットにパイプできますが、 ErrorRecord または Exception オブジェクトが指定されていない限り、結果は異なります。

出力

ErrorRecord

このコマンドレットは、 PSExtendedError オブジェクトを返します。

メモ

PowerShell には、 Get-Errorの次のエイリアスが含まれています。

  • すべてのプラットフォーム:
    • gerr

Get-Error はパイプライン入力を受け入れます。 たとえば、$Error | Get-Error のようにします。