How to get eventlog information in a scheduled task email?

Solution 1:

The only way to accomplish this is a bit of a workaround (in my opinion). You will need to create two actions for your task. The steps are as follows:

  1. Event is triggered
  2. The first action runs a query for the event details you're interested in and saves them to a file.
  3. The second action attaches the event details saved in Step 2 to an e-mail and sends it off.

The process is detailed here:

Solution 2:

That is not the only way to accomplish it. There's actually a much better way that works in windows server 2008 at the very least:

To summarize, you need to export the task definition to xml, add some xpath queries for the data you want in a text editor, update your commandline, and then import your updated task definition back into task scheduler.

Once exported open xml file and find the <EventTrigger> node.

Create child node <ValueQueries>

    <Value name="EventID">Event/System/EventRecordID</Value>
    <Value name="Channel">Event/System/Channel</Value>

You can then reference this value query data in your commandline with $(EventID) and $(Channel). From my testing email events do not substitute these values correctly. Instead you need to execute a commandline. Of course you can add xpath queries to any data in the event, and these are just examples.

You may also experience some challenges in passing certain data from these data queries to a commandline (various characters including quote, slashes, etc... may conflict with the special characters in your command interpreter). To mitigate this i have taken to using the two parameters defined above, and then retrieving the remaining event data using wevtutil:

wevtutil qe "$(Channel)" /q:"*[System[(EventREcordID=$(EventID)]]" /f:xml

you can then do what you want with the full xml text of the event record that generated the alert (such as send an email, parse the xml, etc...)

for example you may define the following action in your task to write the event xml to disk:

    <Arguments>start-transcript -path C:\alertlog.log -append; add-content -path C:\output.txt -value (wevtutil qe "$(Channel)" /q:"*[System[(EventRecordID=$(EventID))]]" /f:xml); stop-transcript;</Arguments>

The referenced blog post goes into greater detail.