gpt4 book ai didi

powershell - 捕获 Get-EventLog 错误

转载 作者:行者123 更新时间:2023-12-02 23:04:06 25 4
gpt4 key购买 nike

我正在编写一个简单的脚本来解析一些事件日志,但我需要在没有结果或 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com