我正在编写一个简单的脚本来解析一些事件日志,但我需要在没有结果或 instanceid 无效时消除一些错误:
PS C:\> get-eventlog Application -instanceid 1111
Get-EventLog : No matches found
At line:1 char:13
+ get-eventlog <<<< Application -instanceid 1111
+ CategoryInfo : ObjectNotFound: (:) [Get-EventLog], ArgumentException
+ FullyQualifiedErrorId : GetEventLogNoEntriesFound,Microsoft.PowerShell.Commands.GetEventLogCommand
我可以这样做并将其静音,但这也会静音其他错误:
PS C:\> try { get-eventlog Application -instanceid 1111 -erroraction stop } catch { }
我试过了,但没用:
PS C:\> try { get-eventlog Application -instanceid 1111 -erroraction stop } catch [ObjectNotFound] { }
Unable to find type [ObjectNotFound]: make sure that the assembly containing this type is loaded.
At line:1 char:91
+ try { get-eventlog Application -instanceid 1111 -erroraction stop } catch [ObjectNotFound] <<<< { }
+ CategoryInfo : InvalidOperation: (ObjectNotFound:String) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
最佳答案
您可以使用 -ErrorAction SilentlyContinue
并在它之后检查您的 $error
变量,
$error[0]
它将始终包含最后一个错误对象。
关于powershell - 捕获 Get-EventLog 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26434831/