Get-Error

현재 세션의 가장 최근 오류 메시지를 가져오고 표시합니다.

Syntax

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

Description

cmdlet은 Get-Error 세션에서 발생한 마지막 오류의 현재 오류 세부 정보를 나타내는 PSExtendedError 개체를 가져옵니다.

Newest 매개 변수를 사용하여 현재 세션에서 발생한 지정된 수의 오류를 표시하는 데 사용할 Get-Error 수 있습니다.

또한 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 cmdlet에 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 를 이 cmdlet으로 파이프할 수 있지만 ErrorRecord 또는 Exception 개체가 제공되지 않는 한 결과가 달라집니다.

출력

ErrorRecord

이 cmdlet은 PSExtendedError 개체를 반환합니다.

참고

PowerShell에는 다음 별칭이 포함됩니다.Get-Error

  • 모든 플랫폼:
    • gerr

Get-Error 는 파이프라인 입력을 허용합니다. 예들 들어 $Error | Get-Error입니다.