Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help! need to read the eventlog in a script, but it refuses to work...

Status
Not open for further replies.

WiccaChic

Technical User
Joined
Jan 21, 2004
Messages
179
Location
US
Hi all. Keep getting the following error on WinXP boxes as well as my Win2000 servers whenever I try to use vbs to read the event log: "
ActiveX component can't create object: 'GetObject'"

Heres the code I am using:

<html>
<head>
<title>OpLets v1.0</title>
</head>
<body>

<script type="text/vbscript">
<!-- event log -->
function eventlog
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'Application'")

For Each objEvent in colLoggedEvents
Wscript.Echo "Category: " & objEvent.Category
Wscript.Echo "Computer Name: " & objEvent.ComputerName
Wscript.Echo "Event Code: " & objEvent.EventCode
Wscript.Echo "Message: " & objEvent.Message
Wscript.Echo "Record Number: " & objEvent.RecordNumber
Wscript.Echo "Source Name: " & objEvent.SourceName
Wscript.Echo "Time Written: " & objEvent.TimeWritten
Wscript.Echo "Event Type: " & objEvent.Type
Wscript.Echo "User: " & objEvent.User
Next
end function

</script>
<p><a href="vbscript:eventlog">Check Event Logs</a> </p>
<hr>
<nobr Id=Results></nobr>
</body>
</html>
 
A web page doesn't have sufficient permission to perform this action. An HTA however can do it. Here's a rewrite, that addresses some other matters as well:

events.hta
Code:
<html>
  <head>
    <title>OpLets v1.0</title>
    <script type="text/vbscript">
Option Explicit

Sub Echo(ByVal strMsg)
  Dim objDIV

  Set objDIV = document.createElement("DIV")
  objDIV.innerText = strMsg
  document.body.appendChild objDiv
  Set objDIV = Nothing
End Sub

'Produce event log listing.
Sub ListEventLog
  Dim strComputer, objWMIService, colLoggedEvents, objEvent

  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

  Set colLoggedEvents = objWMIService.ExecQuery _
      ("Select * from Win32_NTLogEvent Where Logfile = 'Application'")

  For Each objEvent in colLoggedEvents
      Echo "Category: " & objEvent.Category
      Echo "Computer Name: " & objEvent.ComputerName
      Echo "Event Code: " & objEvent.EventCode
      Echo "Message: " & objEvent.Message
      Echo "Record Number: " & objEvent.RecordNumber
      Echo "Source Name: " & objEvent.SourceName
      Echo "Time Written: " & objEvent.TimeWritten
      Echo "Event Type: " & objEvent.Type
      Echo "User: " & objEvent.User
  Next
  Set colLoggedEvents = Nothing
  Set objWMIService = Nothing
  lblStatus.innerText = "Complete!"
  btnCheck.disabled = False
End Sub

Sub btnCheck_onclick
  btnCheck.disabled = True
  lblStatus.innerText = "Working..."
  'Pause 100 ms before starting the hard
  'work, to let the window be updated.
  window.setTimeout "ListEventLog", 100
End Sub
    </script>
  </head>
  <body>
    <input type=button id=btnCheck value="Check Event Logs">
    <span id=lblStatus></span>
    <hr>
  </body>
</html>
Since this script can run for a while, simple feedback was added to indicate when the script is busy.

Note the file name, it must be .HTA, not .HTM or .HTML or something. By adding an <hta:application> tag you can get more control over the window that is displayed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top