Use PowerShell to write to the event log

Whenever writing scripts that are intended to automate something I always want to leave something behind so I can see if the script has run correctly.
Sometimes this can be as easy as a report to a file, e-mail or something like it.

However, in an enterprise environment you propably have a monitoring tool that also monitors the eventlogs of the operating systems… you can use that!
Would’t it be nice to configure your monitoring tools to pick up the your scripts leave behind?
So, the script would need to create an event in the Windows Event Log… Howto do that?

You can simply modify the log source to the name of your script (where the error originated from) and the error ID (1234 in my example) 🙂

And to convert it into a reusable function:

Some of you may correctly think that this is taking the long way around since there is the Write-EventLog cmdlet.
By using this, you can accomplish the same:

And to convert that into a function:

As you can see there are multiple ways to accomplish your goal 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *