Write-EventLog : % 2 output

When using Write-EventLog to write output to the Operations Manager log you get the weird ": % 2" output like this:

Script example:

$Healthy = 2625
$Unhealthy = 2626
$Message = "Rule Name: Test Event API Rule`r"
$Log = "Operations Manager"
$Source = "Health Service Script"
$Warning = "Warning"
$Information = "Information"
Write-EventLog -LogName $Log -Source $Source -EventId $Healthy -EntryType $Information -Message $Message -Category 0

Example:


I think it has something to do with the Health Service Script source expecting 2 parameters. I read somewhere if you add in the -RawData parameter it works but you need to add binary data so not applicable for us. If you register a new source in the ops log like this:

Run this:

New-EventLog -LogName "Operations Manager" -Source "Event API"

Then change this line in the above script:

$Source = "Event API"

Run it again you'll get this output:

Nice and clean now!

Summary

This is just one option if you need to do logging with PowerShell scripts. It's actually more work and there's risk of someone deleting the new source or getting picked up by security teams. The other option (which I use by default) is the LogScriptEvent method with default source Health Service Script, but this has a weird flaw in it where the event needs to be written a certain way. Refer to my other MPs for guidance.

Comments