Get-Error

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

構文

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

説明

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

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

コマンドレットは 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 。 結果は、例 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 をこのコマンドレットにパイプできますが、ErrorRecord オブジェクトまたは Exception オブジェクトが指定されていない限り、結果は異なります。

出力

ErrorRecord

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

メモ

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

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

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