Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Powershell Write-Eventlog The source name test does not exist on computer localhost

I was writing one GUI program on Microsoft Powershell, One of the requirement was logs (Information, Warnings and Errors) must be written under Event Log - Event Viewer, So troubleshooting or information viewing for program operations is easy and everything is logged properly. To fulfill the task there is a PowerShell cmdlet Write-EventLog can be used, but while running the command it was generating the below error. 

Write-Eventlog –LogName Application –Source 'My-App' –EntryType Error –EventID 10999 -Message 'This is test error. Please Ignore'
Write-Eventlog : The source name "My-App" does not exist on computer "localhost".
At line:1 char:1
+ Write-Eventlog –LogName Application –Source 'My-App' –EntryType Error ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Write-EventLog], InvalidOperationException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteEventLogCommand

Microsoft Write-EventLog logname source entrytype error application eventid message write-eventlog the source name does not exist on computer localhost writeeventlogcommand cmdlet.png

This error is generating because I am using my custom source name My-App and it is was not registered or created. Before creating new source event with New-EventLog Make sure you have opened Windows PowerShell as an Administrator. Once new source is registered, I checked writing log with Write-EventLog again, this time it was successful with no output to show on screen. 

New-EventLog –LogName Application –Source 'My-App'

Write-Eventlog –LogName Application –Source 'My-App' –EntryType Error –EventID 10999 -Message 'This is test error. Please Ignore'

Microsoft Powershell scripting New-EventLog -logname application -source myapp wrtie-eventlog -logname system -source Entrytype Error Information warning -eventid -message.png

To verify and check the newly written event log open Event Viewer and verify log is written successfully, You can perform the same task with PowerShell using below command.

Get-EventLog -LogName Application -Newest 5 -EntryType Error -InstanceId 10999

Microsoft Powershell Get-EventLog -logname application system -newest -entrytype Error warning information Instanceid format-list pipeline message username successful.png

If you want to us .net object with PowerShell to substitute New-EventLog, you can use as below.

[System.Diagnostics.EventLog]::CreateEventSource("My-App", "Application")

Useful Articles
Get-PSRepository WARNING Unable to find module repositories
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
Creating an internal PowerShell module repository
How to sign PowerShell ps1 scripts
PowerShell Convert MAC address to Link-local address IPv6
PowerShell Invoke-WebRequest The underlying connection was closed: Could not establish trust relationship for the SSL TLS secure channel.

Go Back

Comment

Blog Search

Page Views

11358489

Follow me on Blogarama