Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - Advanced Event Log Parsing in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Advanced Event Log Parsing in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-05-22

    Table of Contents:
  • Advanced Event Log Parsing in WSH
  • Filtering results
  • More filtering examples
  • Making the code reusable

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Advanced Event Log Parsing in WSH - Filtering results


    (Page 2 of 4 )

    In the last article I challenge you to see if you could create a script that returned only Warning type events.  As promised, we're going to take a look at how to do that.

    First off, we need to figure out how to determine if an event is a Warning type.  We know we can tell that by checking the event’s Type property.  With that in mind, there are two ways to filter our results depending on how we want to handle the information.

    The first way is to adjust the WMI query that we are using so that it only returns the information we want.  We can do by adding a WHERE clause.

    Set colEvents = objWMIService.ExecQuery( _

       "SELECT * FROM Win32_NTLogEvent" WHERE Type = 'Warning'")

    With this code, the WMI service only returns those items that are in the Win32_NTLogEvent class which have a Type property value of “Warning.”  In other words, the collection only contains warning events.

    This can be further filtered to return warnings from a specific log.  Here’s an example:

    Set colEvents = objWMIService.ExecQuery( _

       "SELECT * FROM Win32_NTLogEvent" WHERE Type = 'Warning' " _

       & "AND LogFile = 'System')

    With the above query, only warning events from the System event log are returned in the collection.

    The second way to filter events is by using conditional statements in VBS.  We’re going to return all events in the colEvents collection, and then filter them ourselves so we can handle specific event types differently.

    Set colEvents = objWMIService.ExecQuery( _

       "SELECT * FROM Win32_NTLogEvent")

     

    For Each objEvent In colEvents

       If objEvent.Type = "Warning" Then

           Set objEvent = colEvents.NextEvent()

           objRs.AddNew

           objRs("Category") = objEvent.Category

           objRs("ComputerName") = objEvent.ComputerName

           objRs("EventCode") = objEvent.EventCode

           strMessage = objEvent.Message

           If Len(strMessage) > 100 Then

              strMessage = Left(strMessage, 100)

           End If

           objRs("Message") = strMessage

           objRs("EventType") = objEvent.EventType

           objRs("RecordNumber") = objEvent.RecordNumber

           objRs("SourceName") = objEvent.SourceName

           objRs("TypeDesc") = objEvent.Type

           strUser = objEvent.User

           If IsNull(strUser) Then strUser = "N/A"

           objRs("UserName") = strUser

           objRs("TimeGenerated") = Date2String(objEvent.TimeGenerated)

           objRs("TimeWritten") = Date2String(objEvent.TimeWritten)

           objRs.Update

       End If

    Next

    Here the conditional statements separate event types as part of our For Each…Next loop.  As a result, the colEvents collection still returns all events, but we can react differently based on types.  Here warning events are written to the database.  We could add an Else block to write remaining events to a different database if we wanted.

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · In this article, you'll learn specific ways to customize your event log script for...
     

    WINDOWS SCRIPTING ARTICLES

    - A Portable Scripting Toolbox
    - WPF Through an Example: Introduction
    - Beginning SharePoint Web Part Development
    - More Alternative Languages for WSH
    - WPF Control Layout
    - WSH in Other Languages
    - Screen Capturing via GDI+ and GDI
    - Understanding Procedures in VBScript
    - Printing Documents in WSH
    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...
    - VBScript: More String Functions





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway