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
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'
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
If you want to us .net object with PowerShell to substitute New-EventLog, you can use as below.
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.
This post first appeared on Tales From Real IT System Administrators World And Non-production Environment, please read the originial post: here