fortypoundhead.com

Creating an Event Log with PowerShell

Posted On 2016-12-06 by dwirch
Keywords:
Tags: Powershell Scripting Windows
Views: 605


Programmatically creating an new Windows Event Viewer log can be quite useful during automated processes. While some folks know how this is done, others might needs some guidance. Here's how to do it with PowerShell.

A lot of folks struggle with logging things from within their PowerShell scripts, going down the path of inventing a method of writing events to a text file or database. Why not use the Windows Event Viewer to log things? It's already there, takes care of data file management for you, and can be viewed with standard tools.

As an added bonus, most good monitoring packages such as Splunk or SCOM already provide a method for watching these event logs, and can act on entries therein.

In a "normal" situation, an application or script will write to the Application event log, or possibly the System event log. In my old age, I've taken to logging just about everything that my script or application does, especially while I am in the process of building it. This helps me to debug the script, and instills a good documentation practice. So in order to not clog up the Application event log, I've taken to utilizing custom event logs.

Below is the short script that will build a sample event log on the local machine. Note that the first time it is run, it will need to run with Administrator privileges.

First, let's set some constants up top. These will be used everywhere we want to work with the event log, and helps to prevent typos elsewhere in the script:

Warning! Watch out for line wrap on these code snippets!

 

$LogSource="Create-EventLog.ps1"
$LogName="SampleEventLog"

 

Next, we'll try to create the event log, using the New-EventLog cmdlet, then write the success message to the log. Since we don't want to let the log grow forever, we'll set some limits with the Limit-EventLog cmdlet.

 

try
{
  New-EventLog -LogName $LogName -Source $LogSource -ErrorAction SilentlyContinue
  Write-Eventlog -LogName $LogName -Source $LogSource -EntryType Information -EventId 900 -Message "Create $LogName event log successfully."
  Limit-EventLog -LogName $LogName -RetentionDays 365 -OverflowAction OverwriteOlder -MaximumSize 2GB
}
Catch
{
  Write-Error "Failed to create $LogName event log, $_"
}

 

In the second half of the try/catch, we're simply writing an error, saying that the log could not be created.

Finally, you can now write events to the log with the write-eventlog cmdlet. Below are examples of how you can write both a error and a warning to our shiny new custom event log.

 

Write-eventlog -LogName $LogName -Source $LogSource -EntryType Error -EventId 1001 -Message "This is where your error message would go"

Write-eventlog -LogName $LogName -Source $LogSource -EntryType Warning -EventId 1002 -Message "This is where your warning message would go"

 

And the finished product:

I hope this helps you, and if you have any questions or comments, please post in the comments below.


About the Author

dwirch has posted a total of 174 articles.

You can find more information from dwirch by visiting http://www.derekwirch.com.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.156.39.44

Before you can post, you need to prove you are human. If you log in, this test goes away.



Recent Forum Posts

New security implemented
dwirch posted on July 23, 2017 at about 6:58 in Site News

Fold Code Manager into main KB?
VB6Boy posted on July 22, 2017 at about 14:42 in Site News

Fold Code Manager into main KB?
dwirch posted on July 22, 2017 at about 14:41 in Site News

Fold Code Manager into main KB?
dwirch posted on July 21, 2017 at about 22:46 in Site News

Fold Code Manager into main KB?
dwirch posted on July 20, 2017 at about 7:55 in Site News

Job Spammer: Sam Mallon
dwirch posted on July 18, 2017 at about 18:36 in Spammers