Solved

VBScript to echo event lgo errors only

Posted on 2007-04-01
4
1,617 Views
Last Modified: 2012-06-27
I have a script which will echo event log entry info. I want to change this so it will only echo errors, not information or warnings. Having issues with it and can't get it to work though.

---------------------------------------------------------------------

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NTLogEvent")
For Each objItem in colItems
Wscript.Echo "Category: " & objItem.Category & vbCrLf & "CategoryString: " & objItem.CategoryString & vbCrLf & "ComputerName: " & objItem.ComputerName & vbCrLf & "Data: " & objItem.Data & vbCrLf & "EventCode: " & objItem.EventCode & vbCrLf & "EventIdentifier: " & objItem.EventIdentifier & vbCrLf & "InsertionStrings: " & objItem.InsertionStrings & vbCrLf & "Logfile: " & objItem.Logfile & vbCrLf & "Message: " & objItem.Message & vbCrLf & "RecordNumber: " & objItem.RecordNumber & vbCrLf & "SourceName: " & objItem.SourceName & vbCrLf & "TimeGenerated: " & objItem.TimeGenerated & vbCrLf & "TimeWritten: " & objItem.TimeWritten & vbCrLf & "Type: " & objItem.Type & vbCrLf & "User: " & objItem.User

Next

---------------------------------------------------------------------
0
Comment
Question by:Burns2007
  • 2
4 Comments
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Hey,

You just need to add an If statement to test the Type:


On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NTLogEvent")
For Each objItem in colItems

      If objItem.Type = "error" Then

            Wscript.Echo "Category: " & objItem.Category & vbCrLf & "CategoryString: " &_
                  objItem.CategoryString & vbCrLf & "ComputerName: " &_
                  objItem.ComputerName & vbCrLf & "Data: " & objItem.Data &_
                  vbCrLf & "EventCode: " & objItem.EventCode & vbCrLf &_
                  "EventIdentifier: " & objItem.EventIdentifier & vbCrLf &_
                  "InsertionStrings: " & objItem.InsertionStrings & vbCrLf &_
                  "Logfile: " & objItem.Logfile & vbCrLf & "Message: " &_
                  objItem.Message & vbCrLf & "RecordNumber: " & objItem.RecordNumber &_
                  vbCrLf & "SourceName: " & objItem.SourceName & vbCrLf &_
                  "TimeGenerated: " & objItem.TimeGenerated & vbCrLf & "TimeWritten: " &_
                  objItem.TimeWritten & vbCrLf & "Type: " & objItem.Type & vbCrLf &_
                  "User: " & objItem.User
      End If

Next
0
 
LVL 14

Author Comment

by:Burns2007
Comment Utility
That's what I was trying, but I couldn't get it to work. I tried that script and it doesn't return anything at all.
0
 
LVL 70

Assisted Solution

by:Chris Dent
Chris Dent earned 200 total points
Comment Utility


That implies you have no errors...

You could try removing the "On Error Resume Next" which may point to another problem.

Chris
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 300 total points
Comment Utility
Add the EventType clause to your Select statement.
Set colItems = objWMIService.ExecQuery("Select * from Win32_NTLogEvent WHERE EventType = 1 OR EventType = 2")

I can't remember exactly, but I think event types 1 and 2 are errors and warnings.... there will be docuementation somewhere....

Hope that helps.

Rob.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now