user721015
user721015

Reputation: 47

PowerShell Write-EventLog Not Writing to Desired Event Log

I have the below code (snippet) in a PowerShell function (named Create-EventLog). The end result should be the following:

My PowerShell function snippet:

if (!$EventLogExists) {
    New-EventLog -LogName "Get-Drives" -source "Get-Drives"
    Write-EventLog -LogName "Get-Drives" -source "Get-Drives" -EventId 1 -message "There was an error"
    return
} else {
    Write-EventLog -LogName "Get-Drives" -source "Get-Drives" -EventId 1 -message "There was an error"
    return
}

Running Get-EventLog -List reveals the below output:

enter image description here

Under entries there is a value 1 for the Get-Drives log but it only displays in the Windows Powershell log which also has a value of 1.

Screenshot of the Event Viewer

Get-Drives log: enter image description here

Windows Powershell log: enter image description here

Upvotes: 3

Views: 2899

Answers (2)

Aquiles Toruño
Aquiles Toruño

Reputation: 31

I faced this problem too; it seems to be an issue in EventViewer. I fixed it restarting Windows Event Log Service and recreating de EventSource.

Upvotes: 1

kekimian
kekimian

Reputation: 947

Let's see if your EventLog is created, this command will list all your events log's on local computer:

Get-EventLog -list

If your EventLog is not in the list, try to create it by using just this command and not your function(run as administrator your PS console):

New-EventLog -LogName "Get-Drives" -source "Get-Drives"

Then run again to see if your EventLog has been created.

Get-EventLog -list

If your new EventLog present in the list then run your function to see if now he will be able to write in the right EventLog

Upvotes: 1

Related Questions