Let us begin by taking stock of the operating system's event logs. In our hearts, we know that we should be looking at these logs more often. Also, we know that really we should take action to eradicate those red error messages from our logs.
Thus we have a task for PowerShell's skill set; in fact, we have a marriage made in heaven. PowerShell will help us review the system, application and other logs, while the eventlogs themselves will act as a vehicle for learning more about PowerShell's benefits, capabilities and syntax.
PowerShell Eventlog Topics
• Example 1: Eventlog -list• Example 2: Display error messages from your System log • Example 3: Find Errors in the System log
• Summary of Eventlog Example 1: Eventlog -list
Our first task is to discover how many different logs there are on your machine. Therefore, to discover if your computer has 3, 6, or more logs, append the -list parameter to the get-eventlog command:
# Powershell script to list the event logs. get-eventlog -list
Learning Points
Note 1: You may have guessed that the hash # symbol is PowerShell's way of introducing a comment.
Note2: -list is correct, please note that you do need that dash. Example 2: Display error messages from your System log # Powershell script to find Error messages in the System eventlog. get-EventLog system -newest 2000 | where {$_.entryType -match "Error"} Learning Points
I realize that most people will just copy and paste my script, but for those want to look behind the script, or those who get stuck, I provide 'Learning Points'. My greatest joy is if you would
experiment with my code, for example, change 2000 to 10000; or more adventurously, change get- eventlog system to get-eventlog application.
Note 1: You could simplify the script further and just type: get-EventLog system
Note 2: Each word, and indeed every symbol, has deep meaning to PowerShell. (|) pipes the output of the first clause into the 'Where' statement. As a result the output is filtered so that you see only error messages, and not information or warning messages.
Note 3: PowerShell supports a whole family of conditional statements, for example, -like, -contains, or even plain -eq (Equals), but for this job, I chose -match.
Trusty Twosome (Get-Help and Get-Member)
Whenever you discover a new PowerShell command, it benefits from being surveyed with what I call the 'Trusty Twosome'. In this instance, if you research a verb-noun command with Get-Help and Get-Member, then you are sure to unearth new scripting possibilities. To see what I mean, apply these two commands to get-Eventlog:
1) get-help get-Eventlog
If you want to see examples of the get-Eventlog in action try: help eventlog -full.
Get-help displays useful parameters such as: -list, -logname, and -newest. Indeed, the first thing to remember about get-Eventlog is that it needs the name of the log, for example: get-Eventlog system. Remember that PowerShell is looking for a positional argument, thus 'system' is the name of the log and is not a parameter. To determine this difference, PowerShell expects a parameter to be
introduced with a -dash, whereas an argument is preceded by only a space.
Other names of logs that you can substitute for 'system' are: Application, Security and even PowerShell itself. Windows Server 2003 is likely to have yet more logs, for example, Directory Service and DNS Server.
2) get-eventlog system |get-member -memberType property
If you wish to filter gm try: get-eventlog system | gm -membertype property.
The above command reveals a list of properties that you can then use in the output, for example, category and source.
Example 3: Errors in the System log
This example produces a very similar result to example 1 above. The whole point of the extra code is to give us more control over the output. There are numerous ways that you could achieve the same list of events, indeed, many of them are technically superior to mine; however, I felt strongly that this script should demonstrate useful PowerShell features such as: $Variables, pipeline, format-table and the tiny ` backtick.
# Cmdlet to find latest 2000 errors in the System eventlog $SysEvent = get-EventLog -logname system -newest 2000 $SysError = $SysEvent |where {$_.entryType -match "Error"} $SysError | sort eventid | `
Learning Points
Note 1: Guy loves variables. In PowerShell you just declare variables with a $dollar sign, there is nothing else you need to do.
Note 2: The first example employed one pipeline (|), whereas this script has three (|)s. This
technique of using the output of the first clause as the input of the second clause, is a characteristic benefit of PowerShell.
Challenge 1: I chose to sequence the data with: sort eventid. Now, I challenge you to sort on TimeWritten.
Challenge 2: In my opinion, it's not necessary to include entryType in the Format-Table statement, but I challenge you to add it, and then see if I am right, or see if I am wrong to omit this property. Challenge 3: I used the backtick ` to run one command over two lines. You could try removing the backtick and making fewer, but longer lines. Other experiments that you could try with backtick include putting a ` at a different point in the script. Even better, try for one long but efficient command, perhaps use only one variable.
Summary of Eventlog
I believe that PowerShell has a future. My mission is to get you started using PowerShell. What suits my learning style is concrete examples, where we learn by doing. It is my belief that a good way to begin is by employing PowerShell to tackle everyday tasks such as reviewing the eventlogs.
Just by issuing a few variations of the command 'get-eventlog system', you will soon get a feeling of the abilities of PowerShell. Moreover, as a bonus you will soon obtain useful information about events in your operating system. The command: 'get-eventlog application' has a wide range of switches, for example -list and -newest. What is always instructive with any PowerShell command, is get-member, for example: