SOLUTION – Write-EventLog : The source name X does not exist on computer “localhost”

While attempting to create a new event log using write-eventlog cmdlet you may hit this error if the Source is not already registered.

New-EventLog can be used to add anew source, following is the example


#
# Sample Write event 
Write-EventLog -LogName Application -Source "myNewSource" -EventID 3001 -Message "myNewSource new message"

# Sample Error
# Write-EventLog : The source name "myNewSource" does not exist on computer  "localhost".

# View available Source names registered, if anything available can be used 
(Get-WmiObject -Class Win32_NTEventLOgFile |
  Select-Object FileName, Sources |
  ForEach-Object -Begin { $hash = @{}} -Process { $hash[$_.FileName] = $_.Sources } -end { $Hash })["Application"]

# Solution, Add new Source Naem 
New-EventLog -LogName Application -Source "myNewSource"


#Verify if new source is registed
(Get-WmiObject -Class Win32_NTEventLOgFile |
  Select-Object FileName, Sources |
  ForEach-Object -Begin { $hash = @{}} -Process { $hash[$_.FileName] = $_.Sources } -end { $Hash })["Application"] | findstr "myNewSource"


# Write new event - Success 
Write-EventLog -ComputerName "Server01" -LogName Application -Source "MyApp" -EventID 3001 -Message "MyApp added a user-requested feature to the display."

#
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s