Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 812
Chase Florell Avatar asked Dec 13 '12 01:12

Chase Florell


People also ask

How do I find EventLog?

The Get-EventLog cmdlet gets events and event logs from local and remote computers. By default, Get-EventLog gets logs from the local computer. To get logs from remote computers, use the ComputerName parameter. You can use the Get-EventLog parameters and property values to search for events.

What is EventLog source?

The event source indicates what logs the event. It is often the name of the application, or the name of a subcomponent of the application, if the application is large. Applications and services should write to the Application log or a custom log. Device drivers should write to the System log.

Which EventLog method do you call to remove all entries from an existing event log?

The Remove-EventLog cmdlet deletes an event log file from a local or remote computer and unregisters all its event sources for the log.

Which of the following methods can be used to delete an EventLog object?

You can use the static members of EventLog to delete logs, get log lists, create or delete a source, or determine if a computer already contains a particular source. There are three default event logs: Application, System, and Security.


2 Answers

# Check if Log exists # Ref: http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.exists(v=vs.110).aspx [System.Diagnostics.EventLog]::Exists('Application');   # Ref: http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.sourceexists(v=vs.110).aspx # Check if Source exists [System.Diagnostics.EventLog]::SourceExists("YourLogSource"); 
like image 173
Jonathan Donahue Avatar answered Sep 23 '22 15:09

Jonathan Donahue


So I was on the right path with Get-EventLog.

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" } 
like image 22
Chase Florell Avatar answered Sep 22 '22 15:09

Chase Florell