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.
Locked
User avatar
webward
Posts: 1
Joined: Mon Dec 07, 2015 5:57 pm

PowerShell Studio: Windows Service Event Log Entries

Post by webward » Mon Nov 04, 2019 10:26 am

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
Site Admin
Posts: 368
Joined: Sun Mar 03, 2013 12:42 pm

Re: PowerShell Studio: Windows Service Event Log Entries

Post by mxtrinidad » Mon Nov 04, 2019 11:12 am

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: 14009
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell Studio: Windows Service Event Log Entries

Post by jvierra » Mon Nov 04, 2019 11:15 am

Max, it is easier than that:

Write-EventLog -LogName application -EntryType Error -Message <pass error here>

User avatar
mxtrinidad
Site Admin
Posts: 368
Joined: Sun Mar 03, 2013 12:42 pm

Re: PowerShell Studio: Windows Service Event Log Entries

Post by mxtrinidad » Mon Nov 04, 2019 11:24 am

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: 14009
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell Studio: Windows Service Event Log Entries

Post by jvierra » Mon Nov 04, 2019 11:38 am

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: 7122
Joined: Tue May 29, 2007 4:43 pm

Re: PowerShell Studio: Windows Service Event Log Entries

Post by Alexander Riedel » Mon Nov 04, 2019 11: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.
Alexander Riedel
SAPIEN Technologies, Inc.

jvierra
Posts: 14009
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell Studio: Windows Service Event Log Entries

Post by jvierra » Mon Nov 04, 2019 11:50 am

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: 14009
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell Studio: Windows Service Event Log Entries

Post by jvierra » Mon Nov 04, 2019 11:53 am

Alexander Riedel wrote:
Mon Nov 04, 2019 11: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.

Locked