Solved

VBScript to echo event lgo errors only

Posted on 2007-04-01
4
1,620 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
ID: 18834766

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
ID: 18835666
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
ID: 18836089


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
ID: 18887138
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 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