Reputation: 47
I have the below code (snippet) in a PowerShell function (named Create-EventLog). The end result should be the following:
Get-Drives
were PowerShell writes all exception messages for this application. The Get-Drives
event log gets created but for some reason
the actual message(s) are only getting written in the default Windows PowerShell
event log. How can I make PowerShell write the messages to the "Get-Drives" event log instead of the "Windows PowerShell" event log? I'm using PowerShell 4 and using PowerShell tools in Visual Studio 2013.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:
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
Upvotes: 3
Views: 2899
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
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