Question: How to write an event log entry with structured XML data using PowerShell?
My PowerShell script writes to the Windows event log using the Write-EventLog
cmdlet. Currently I use the -Message
parameter to set the event log message:
Write-EventLog -LogName $EventLogName -Source $EventSource -EntryType Error -EventId 1 -Message "MyMessageHere"
If you look at the message using Windows EventViewer you get an XML like this:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
[...]
</System>
<EventData>
<Data>MyMessageHere</Data>
</EventData>
</Event>
I.e. the message is set as event data. Now I want to write structured event data, where the contents of the Data element is XML (see your own Windows\Security log for an example).
I tried using Write-EventLog
as follows: -Message "<Data Name=""MyKey1"">MyValue1</Data>
but that does not work properly, it looks like the message is added as CDATA to the inside the Data element.
So, how to write an event log entry with structured XML data using PowerShell?