Get-Error
取得並顯示目前會話的最新錯誤訊息。
語法
Get-Error
[[-Newest] <Int32>]
[<CommonParameters>]
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 參數。 在此範例中,Newest 會傳回此工作階段中 3 個最新錯誤的詳細數據。
Get-Error -Newest 3
範例 3:傳送錯誤集合以接收詳細訊息
$Error
自動變數包含目前工作階段中的錯誤物件陣列。 對象的陣列可以透過管道傳送至 Get-Error
,以接收詳細的錯誤訊息。
在此範例中,$Error
會管線傳送至 Get-Error
Cmdlet。 結果是詳細的錯誤訊息清單,類似於範例 1 的結果。
$Error | Get-Error
參數
-InputObject
此參數用於管線輸入。
類型: | PSObject |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | True |
接受萬用字元: | False |
-Newest
指定要顯示目前工作階段中發生的錯誤數目。
類型: | Int32 |
別名: | Last |
Position: | 0 |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
輸入
PSObject
您可以使用管線將任何 PSObject 傳送至此 Cmdlet,但除非提供 ErrorRecord 或 Exception 對象,否則結果會有所不同。
輸出
此 Cmdlet 會傳回 PSExtendedError 物件。
備註
PowerShell 包含下列 Get-Error
別名:
- 所有平臺:
gerr
Get-Error
接受管線輸入。 例如,$Error | Get-Error
。