Techibee.com

Powershell: Write-EventLog Error “Write-EventLog : The source name XXX does not exist on computer

If you are using Write-EventLog to write any new events to event log, it is possible that you might see below error.

Write-EventLog : The source name “XXX” does not exist on computer “localhost”.

I am into similar situation today. What I was doing is trying to write a new event in to Application event log with a Event Source that is not already exists in the computer where I am trying. So the error message makes sense. It is saying the source I am trying to use, XXX doesn’t exists. So, how should get this added to list of available event sources in the computer so that it won’t through the error message again.

After little bit of googling, I found the below method promising.

New-EventLog -LogName Application -Source “XXX”

This command creates a new event source in Application Event log with the name XXX. All I need to do is just use this command once and try the write-EventLog cmdlet with the same source name(XXX). It worked this time.

Hope this little trick helps you…

[Update]

There are some dotnet ways available as well to see if a given source is available in EventLog and create one if needed.

To check if the source exists:

PS C:\> [system.diagnostics.eventlog]::SourceExists(“XXX”)
False
PS C:\>

To create a new source entry in event log:

[system.diagnostics.EventLog]::CreateEventSource(“XXX”, “Application”)

 

Exit mobile version