Get-Error
接收並顯示目前會話中最新的錯誤訊息。
語法
Newest (預設值)
Get-Error
[[-Newest] <Int32>]
[<CommonParameters>]
Error
Get-Error
[-InputObject <PSObject>]
[<CommonParameters>]
Description
指令 Get-Error 檔會取得一個 PSExtendedError 物件,代表該會話中最後一次錯誤的當前錯誤細節。
你可以用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 回傳了本次會話中發生的最新三個錯誤細節。
Get-Error -Newest 3
範例 3:發送一組錯誤以接收詳細訊息
$Error自動變數包含目前會話中的錯誤物件陣列。 物件陣列可透過管道 Get-Error 接收詳細錯誤訊息。
在此範例中, $Error 是 被 pipe 到 Get-Error cmdlet。 結果是詳細錯誤訊息的清單,類似範例 1 的結果。
$Error | Get-Error
參數
-InputObject
此參數用於管線輸入。
參數屬性
| 類型: | PSObject |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
Error
| Position: | Named |
| 必要: | False |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Newest
指定當前會話中要顯示的錯誤數量。
參數屬性
| 類型: | Int32 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 最後 |
參數集
Newest
| Position: | 0 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
PSObject
你可以將任何 PSObject 管道傳送到這個 cmdlet,但除非提供 ErrorRecord 或 Exception 物件,結果會有所不同。
輸出
ErrorRecord
此 cmdlet 回傳一個 PSExtendedError 物件。
備註
PowerShell 包含下列 Get-Error的別名:
- 所有平臺:
gerr
Get-Error 接受管線輸入。 例如: $Error | Get-Error 。