如何确定一个EventLog是否已经存在 [英] How to determine if an EventLog already exists
问题描述
我正在使用以下行来创建新的事件日志
I'm using the following line to create a new event log
new-eventlog -LogName "Visual Studio Builds" -Source "Visual Studio"
我想每次都运行此命令,因为如果我是从新计算机上运行构建,我仍然希望查看事件日志.
I want to run this every time, because if I run a build from a new computer, I'd still like to see the event logs.
问题在于,在创建日志后每次运行脚本时,都会引发错误.
The problem is that every time the script is run after the log is already created, it throws an error.
New-EventLog : The "Visual Studio" source is already registered on the "localhost" computer.
At E:\Projects\MyApp\bootstrap.ps1:14 char:13
+ new-eventlog <<<< -LogName "Visual Studio Builds" -Source "Visual Studio"
+ CategoryInfo : InvalidOperation: (:) [New-EventLog], InvalidOperationException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.NewEventLogCommand
现在我知道我可以搜索"事件日志了
Now I know that I can "search" for the event log
Get-EventLog -list | Where-Object {$_.logdisplayname -eq "Visual Studio Builds"}
但是现在我如何确定它是否存在?
But now how do I determine if it exists?
推荐答案
所以我在Get-EventLog
的正确位置上.
So I was on the right path with Get-EventLog
.
我将其存储在一个变量中,而不仅仅是阅读它.然后,我检查了变量是否为null
.
Instead of just reading it, I stored it in a variable. Then I checked if the variable was null
.
这已经实现了我想要做的事情.
This has achieved what I was looking to do.
$logFileExists = Get-EventLog -list | Where-Object {$_.logdisplayname -eq "Visual Studio Builds"}
if (! $logFileExists) {
New-EventLog -LogName "Visual Studio Builds" -Source "Visual Studio"
}
这篇关于如何确定一个EventLog是否已经存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!