Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Event Log Parsing for the WSH Administrato...
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 
Mobile Linux 
App Generation ROI 
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

Event Log Parsing for the WSH Administrator
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-05-23

    Table of Contents:
  • Event Log Parsing for the WSH Administrator
  • Modularizing the script
  • Polling machines across a network
  • Monitoring event logs

  • 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


    Event Log Parsing for the WSH Administrator - Monitoring event logs


    (Page 4 of 4 )

    Let’s take a look at how to create a logon script that monitors the event logs.  We’re going to add an extra feature to have the script send an email notification if it detects a warning or error event.

    strComputer = "."

     

    Set objWMIService = GetObject("winmgmts:{(Security)}" & strComputer _

       & "rootcimv2")

    Set colEvents = objWMIService.ExecNotificationQuery _

       ("SELECT * FROM __InstanceCreationEvent WHERE " _

           & "TargetInstance ISA 'Win32_NTLogEvent' " _

           & "AND TargetInstance.Type = 'Warning' " _

           & "OR TargetInstance.Type = 'Error'")

    Do While True

       Set objEvent = colEvents.NextEvent()

       strUser = objEvent.User

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

       strMessage = objEvent.Message

       If Len(strMessage) > 100 Then

           strMessage = Left(strMessage, 100)

       End If

     

       return = EmailNotify(objEvent.Category, _

           objEvent.ComputerName, _

           objEvent.EventCode, _

           strMessage, _

           objEvent.EventType, _

           objEvent.RecordNumber, _

           objEvent.SourceName, _

           objEvent.Type, _

           strUser, _

           DateTime2String(objEvent.TimeGenerated), _

           DateTime2String(objEvent.TimeWritten))

    Loop

    The base script looks like this.  It connects to the WMI Service and issues an Event Notification Query.  This script utilizes event scripting.  That means our script will only be executed when a new event occurs that matches our parameters.  This script will run indefinitely any time the computer is turned on.

    If the event is a Warning or Error type event, the event’s details are passed to the EmailNotify function.

    Function EmailNotify(intCategory, strComputerName, intEventCode, strMessage, strEventType, intRecordNumber, strSourceName, strTypeDesc, strUserName, strTimeGenerated, strTimeWritten)

       strBody = "Category: " & CStr(intCategory) & VbCrLf

       strBody = strBody & "ComputerName: " & strComputerName & VbCrLf

       strBody = strBody & "EventCode: " & CStr(intEventCode) & VbCrLf

       strBody = strBody & "Message: " & strMessage & VbCrLf

       strBody = strBody & "EventType: " & strEventType & VbCrLf

       strBody = strBody & "RecordNumber: " & CStr(intRecordNumber) & VbCrLf

       strBody = strBody & "SourceName: " & strSourceName & VbCrLf

       strBody = strBody & "TypeDesc: " & strTypeDesc & VbCrLf

       strBody = strBody & "User: " & strUserName & VbCrLf

       strBody = strBody & "TimeGenerated: " & strTimeGenerated & VbCrLf

       strBody = strBody & "TimeWritten: " & strTimeWritten & VbCrLf

     

       Set objEmail = CreateObject("CDO.Message")

       objEmail.From = "Notify Script"

       objEmail.To = "admin@mymail.com"

       objEmail.Subject = "New Event Notification"

       objEmail.Textbody = strBody

       objEmail.Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

       objEmail.Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _

           "smtp.mymail.com"

       objEmail.Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

       objEmail.Configuration.Fields.Update

       objEmail.Send

       Set objEmail = Nothing

       EmailNotify = 1

    End Function

    This function assembles the event details into an email message that is sent to an administrator using CDO.  Be sure to change both the destination address and the SMTP server to match your configuration.

    Function Date2String(objTime)

        yyyy = Left(objTime, 4)

        mm = Mid(objTime, 5, 2)

        dd = Mid(objTime, 7, 2)

        hh = Mid(objTime, 9, 2)

        min = Mid(objTime, 11, 2)

        sec = Mid(objTime, 13, 2)

        Date2String = mm & "/" & dd & "/" & yyyy & " " & hh & ":" & min & ":" & sec

    End Function

    You’ll also need the Date2String function to create friendly date and time stamps.

    There you have it.  Two scripts that a network administrator can use to monitor or track the event logs for machines within their network.  Making use of scripts like this will improve your efficiency and keep you in closer contact with the machines you service.

    Good luck maintaining your machines.  Until next time…keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Wrapping up this 3-part series, I wanted to demonstrate ways for network...
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT