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.
PowerShell Studio: Windows Service Event Log Entries
Forum rules
Do not post any licensing information in this forum.
Do not post any licensing information in this forum.
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: PowerShell Studio: Windows Service Event Log Entries
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.
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.
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: PowerShell Studio: Windows Service Event Log Entries
I had issues with that cmdlet so I ended up using custom code as it was easier for me.
But, the cmdlet is available.
But, the cmdlet is available.
Re: PowerShell Studio: Windows Service Event Log Entries
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.
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.
- Alexander Riedel
- Posts: 8479
- Last visit: Thu Mar 28, 2024 9:29 am
- Been upvoted: 37 times
Re: PowerShell Studio: Windows Service Event Log Entries
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.
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.
SAPIEN Technologies, Inc.
Re: PowerShell Studio: Windows Service Event Log Entries
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.
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.
Re: PowerShell Studio: Windows Service Event Log Entries
That would be the best solution and would be what I would expect.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.