Solved

VBScript to echo event lgo errors only

Posted on 2007-04-01
4
1,619 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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