PowerShell Studio: Windows Service Event Log Entries

Post feature requests, product enhancement ideas, and other product-specific suggestions here. Do not post bug reports.
Forum rules
Do not post any licensing information in this forum.
This topic is 4 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
webward
Posts: 1
Last visit: Sun Mar 08, 2020 9:11 am

PowerShell Studio: Windows Service Event Log Entries

Post by webward »

I did some testing with a Windows Service project in PowerShell Studio, I appreciate that certain outputs are automatically sent to the event log, the Information stream creates an Info message, the Warning stream creates a Warning message, but I noticed that Write-Error does not produce an error message in the logs.

It would be a very nice to have feature if Write-Error produced error messages in the Windows event logs.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: PowerShell Studio: Windows Service Event Log Entries

Post by mxtrinidad »

This is a Microsoft PowerShell cmdlet - " The `Write-Error` cmdlet declares a non-terminating error. By default, errors are sent in the error stream to the host program to be displayed, along with output."

Feel free to use their GitHub PowerShell Repository to provide feedback.

To create a ways to write to the Eventlog, check out the .Net System.Diagnostics.Eventlog() class.
You could start by creating a simple function like:

function WriteTo-MyEventlog
{
[CmdletBinding()]
param ()

## - Write to the Windows PowerShell Eventlog:
[string]$EventResult = ($SystemInfoObj | Format-List | Out-String -Width 1000);

## - Values provided: LogName, ComputerName, Source
$Eventlog = New-Object `
System.Diagnostics.Eventlog("Application", $env:COMPUTERNAME, `
"Custom Script Get-SystemInformation");


## - Values provided: Message, EntryLevel, EventID
$Eventlog.WriteEntry("Output Result: $EventResult", 'Information', '100');

};

Then, polish it to suit your need.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell Studio: Windows Service Event Log Entries

Post by jvierra »

Max, it is easier than that:

Write-EventLog -LogName application -EntryType Error -Message <pass error here>
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: PowerShell Studio: Windows Service Event Log Entries

Post by mxtrinidad »

I had issues with that cmdlet so I ended up using custom code as it was easier for me.
But, the cmdlet is available.
:)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell Studio: Windows Service Event Log Entries

Post by jvierra »

It works but you cannot use undefined sources. If you leave to source off it will default correctly.

Just use the following:

Write-EventLog -LogName application -EntryType Error -Message '<pass error here>' -Source Application -EventId 0x8001

You can also register a custom source for the service but you should use high IDs to avoid conflicts in the log. There are some rules about allocating IDs that are documented for application developers to allocate IDs that eliminate conflicts.

No matter which method you use the source and ID should be deconflicted. IDs above 32767 (0x8000 and greater) are considered either test or experimental if the based on the two high bits.

Custom event sources are advisable for services and should be created by the service installer.

In modern event logs it is much easier and more useful to create a custom log for events.

New-Eventlog
See: https://docs.microsoft.com/en-us/powers ... rshell-5.1

The service event log should also be created by the service installer.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: PowerShell Studio: Windows Service Event Log Entries

Post by Alexander Riedel »

The service host for the packager overrides WriteErrorLine(string value) as

public override void WriteErrorLine(string value)
{
m_EventLog.WriteEntry(value, EventLogEntryType.Error,1000);
}

So Write-Error *should* write to the event log. I will try to verify this when I have a minute.
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell Studio: Windows Service Event Log Entries

Post by jvierra »

We should also note that the "Write-Error" command does not write and really shouldn't be called "Write". It should be "Throw" - "Throw-Error" which is h=what the command does. It generates an exception.

The MS service documentation specifies that all exceptions need to be caught and written to the services event source as "Error".

I have not checked to see if Sapien's code wrapper does this. I think it does so just throwing an exception should cause it to be logged. If not then use of a Try/Catch with the catch throwing the error should cause a log message to be generated.

Managing service excepti9ons can be tricky.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell Studio: Windows Service Event Log Entries

Post by jvierra »

Alexander Riedel wrote: Mon Nov 04, 2019 10:43 am The service host for the packager overrides WriteErrorLine(string value) as

public override void WriteErrorLine(string value)
{
m_EventLog.WriteEntry(value, EventLogEntryType.Error,1000);
}

So Write-Error *should* write to the event log. I will try to verify this when I have a minute.
That would be the best solution and would be what I would expect.
This topic is 4 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked