Answered by:
Write a Powershell Output to Windows Event Viewer using Write-EventLog not providing the expected results.

Question
-
Hi All,
I have a requirement where i need to write the output of a Powershell script to the Event log.
I have tried few methods with no success or issues with the format.
Below is the script:
New-EventLog –LogName "Operations Manager" –Source "Custom Exchange Monitoring"
Import-Module Operationsmanager
$Proxyenabled = Get-SCOMAgent | Where-Object {$_.ProxyingEnabled -Like "*False*"} | Select Displayname, ProxyingenabledIf ($Proxyenabled -ne $null)
{$Test1 = $Proxyenabled.Displayname
$Test2 = $Proxyenabled.ProxyingEnabled
$Test3 = "Agent proxying not enabled on the following servers `n $Test1 `n $Test2"
Write-EventLog -LogName "Operations Manager" –Source "Custom Exchange Monitoring" -EntryType Error -EventID 1234 -Message "$Test3"
}
Else
{
Write-Host "All Agents have Proxying Enabled"
}Output of - $Proxyenabled = Get-SCOMAgent | Where-Object {$_.ProxyingEnabled -Like "*False*"} | Select Displayname, Proxyingenabled
What i get in Event viewer:
Does any one know if it is possible to format this data and write it in a proper readable manner in Powershell or exactly replicate the powershell output in the Write-EventLog command ?
Gautam.75801
Wednesday, January 2, 2019 9:53 PM
Answers
-
In this forum you must post properly formatted code usi g the code posting tool provided on the edit bar.
You cannot write or create a product event log.
Read the help for New/Write-EventLog. It gives examples of writing to the application log and instructions on writing to a custom event log.
help Write-Eventlog -online
help New-Eventlog -onlineFor a new log you will need to supply a resource DLL or only use simple messages. THe DLL has a special com piler that creates formatted message templates.
\_(ツ)_/
- Edited by jrv Wednesday, January 2, 2019 10:09 PM
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Thursday, January 3, 2019 2:36 AM
- Marked as answer by Gautam RMicrosoft employee Friday, January 4, 2019 12:42 AM
Wednesday, January 2, 2019 10:06 PM -
For resource DLL creation see: https://docs.microsoft.com/en-us/windows/desktop/eventlog/message-files
\_(ツ)_/
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Thursday, January 3, 2019 2:36 AM
- Marked as answer by Gautam RMicrosoft employee Friday, January 4, 2019 12:42 AM
Wednesday, January 2, 2019 10:10 PM -
Just to update, My colleague helped me and we got it achieved as below:
$api=New-Object -comObject MOM.ScriptAPI Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; $Exchangeservername = $env:computername $results = Get-ExchangeServer | Where-Object {$_.Name -Eq "$Exchangeservername"} | Get-Queue | Select Identity, MessageCount foreach($result in $results) { [string]$Name = $result.Identity.Server [string]$QueueName = $result.Identity.Type [string]$ServerQueue = $Name + "\" + $QueueName [int]$MessageCount = $result.MessageCount IF ($MessageCount -gt 0) { #MessageCount is greater than zero $api.logscriptevent("Messagequeuemonitor.Ps1",4368,1,"Exchange Server has a message queue above 1 `r`n ServerName: ($Name) QueueName: ($QueueName) MessageCount: ($MessageCount)") } ELSE { #MessageCount is missing or equal zero } }
The ones in Bold and Italic is just an API the Monitoring tool can utilise to pass the output and the API will write the data in the Event log.
The script is different what is used here to work but the powershell command let used to provide me a multi line output and a foreach loop did simple job.
Gautam.75801
- Marked as answer by Gautam RMicrosoft employee Friday, January 4, 2019 12:41 AM
- Edited by Gautam RMicrosoft employee Friday, January 4, 2019 12:44 AM
Friday, January 4, 2019 12:41 AM
All replies
-
In this forum you must post properly formatted code usi g the code posting tool provided on the edit bar.
You cannot write or create a product event log.
Read the help for New/Write-EventLog. It gives examples of writing to the application log and instructions on writing to a custom event log.
help Write-Eventlog -online
help New-Eventlog -onlineFor a new log you will need to supply a resource DLL or only use simple messages. THe DLL has a special com piler that creates formatted message templates.
\_(ツ)_/
- Edited by jrv Wednesday, January 2, 2019 10:09 PM
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Thursday, January 3, 2019 2:36 AM
- Marked as answer by Gautam RMicrosoft employee Friday, January 4, 2019 12:42 AM
Wednesday, January 2, 2019 10:06 PM -
For resource DLL creation see: https://docs.microsoft.com/en-us/windows/desktop/eventlog/message-files
\_(ツ)_/
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Thursday, January 3, 2019 2:36 AM
- Marked as answer by Gautam RMicrosoft employee Friday, January 4, 2019 12:42 AM
Wednesday, January 2, 2019 10:10 PM -
Just to update, My colleague helped me and we got it achieved as below:
$api=New-Object -comObject MOM.ScriptAPI Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; $Exchangeservername = $env:computername $results = Get-ExchangeServer | Where-Object {$_.Name -Eq "$Exchangeservername"} | Get-Queue | Select Identity, MessageCount foreach($result in $results) { [string]$Name = $result.Identity.Server [string]$QueueName = $result.Identity.Type [string]$ServerQueue = $Name + "\" + $QueueName [int]$MessageCount = $result.MessageCount IF ($MessageCount -gt 0) { #MessageCount is greater than zero $api.logscriptevent("Messagequeuemonitor.Ps1",4368,1,"Exchange Server has a message queue above 1 `r`n ServerName: ($Name) QueueName: ($QueueName) MessageCount: ($MessageCount)") } ELSE { #MessageCount is missing or equal zero } }
The ones in Bold and Italic is just an API the Monitoring tool can utilise to pass the output and the API will write the data in the Event log.
The script is different what is used here to work but the powershell command let used to provide me a multi line output and a foreach loop did simple job.
Gautam.75801
- Marked as answer by Gautam RMicrosoft employee Friday, January 4, 2019 12:41 AM
- Edited by Gautam RMicrosoft employee Friday, January 4, 2019 12:44 AM
Friday, January 4, 2019 12:41 AM -
Yes. That works if you want to write to the MOM/SCOM event provider. It also o ly works on a server where the SDK is installed or SCOM is installed.
If that is what you wanted then you are set.
\_(ツ)_/
Friday, January 4, 2019 1:07 AM -
You are right...
Gautam.75801
Friday, January 4, 2019 12:20 PM