Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Writing to the Eventlog

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2904
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Writing to the Eventlog
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Writing to the Eventlog - 5/5/2005 9:19:08 PM   
  willem

 

Posts: 12
Score: 0
Joined: 4/25/2005
From: France
Status: offline
Hi co-scripters,

I have a problem to tidy up my script to write to the eventlog. I am switching a lot between Windows2000 (my desktop) and the server, which is Windows2003. As the "eventcreate.exe" does not exist for Windows2000, I have to find another solution:
-either find a way to define the Operating System dynamically (I now do it statically, but forget sometimes)
-or use some other method to write the entries (I tried the class System.Diagnostics.Eventlog, see bottom of code, but probably making a mistake somewhere.)

I want to use the EventID, which is not possible with the WShell.LogEvent method, which is what I am now using for Windows2000. I don't want to make a eventcreate.bat file on my desktop, that is definitely not a tidy way to do it.

Can you help me with this? Here is what I have got now:

Const icWINDOWS2000=1
Const icWINDOWSXP=2
'************************
'* Eventlog entry types
'************************
Const icSUCCESS=0
Const icERROR=1
Const icWARNING=2
Const icINFORMATION=4
Const icAUDIT_SUCCESS=8
Const icAUDIT_FAILURE=16
dim iOS
dim oShell, oEvtLog
iOS=icWINDOWS2000 'this should be done dynamically!
'*** error 33 to be logged somewhere in the program:
ActionLog icERROR, "There is an error...", 33

'******************************************************************
'* Subroutine ActionLog *
'******************************************************************
Sub ActionLog ( iSeverity, sDescription, iEvtID )
Const scSource="MyProgram"
Dim bTryLog, bTryShell, strcommand
'**********************************
'* Echo to the console
'**********************************
Select Case iSeverity
Case icSUCCESS
sSeverity="SUCCESS"
Case icERROR
sSeverity="ERROR"
Case icWARNING
sSeverity="WARNING"
Case icINFORMATION
sSeverity="INFORMATION"
Case icAUDIT_SUCCESS
sSeverity="AUDIT_SUCCESS"
Case icAUDIT_FAILURE
sSeverity="AUDIT_FAILURE"
Case Else
sSeverity=""
End Select
If (bRunMode=bcRUN_DEV) Then wscript.echo sSeverity & ": " & sDescription
'**********************************
'* Log in the Event Log
'**********************************
bTryShell=False
On Error Resume Next
bTryShell=(oShell is Nothing)
If bTryShell Then
Set oShell=CreateObject("WScript.Shell")
End If
if (iOS=icWINDOWS2000) then
oShell.LogEvent iSeverity, sDescription
else
strCommand = "eventcreate /T " & sSeverity & " /ID " & CStr(iEvtID) & " /L APPLICATION /SO " & scSource & " /D " & Chr(34) & sDescription & Chr(34)
oShell.Run strcommand
endif
'**************************************************
'* Log in the Event Log using Class - does not work
'**************************************************
' bTryLog=False
'On Error Resume Next
' bTryLog=(oEvtLog is Nothing)
' If bTryLog Then
' Set oEvtLog=CreateObject("System.Diagnostics.EventLog")
' End If
' oEvtLog.WriteEntry scSource, sDescription, iSeverity, iEvtID
End Sub


*****************************
Willem
 
 
Post #: 1
 
 Re: Writing to the Eventlog - 5/5/2005 9:46:58 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
if instr(getOSver,"Server 2003") then
iOS = icWINDOWSXP
else
iOS = icWINDOWS2000
end if

=================================================
function getOSver
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Set os = wmi.execquery("select * from win32_operatingsystem")
For Each os1 In os
getOSver = os1.caption
Next
end function

(in reply to willem)
 
 
Post #: 2
 
 Re: Writing to the Eventlog - 5/5/2005 11:03:32 PM   
  willem

 

Posts: 12
Score: 0
Joined: 4/25/2005
From: France
Status: offline
OK, this will solve my immediate problem, thanks Token.

Is this then the correct way to implement writing to the EventLog?
It seems to me there should be a COM class available, instead of a commandline tool.
There is the one used for Windows 2000 (WShell.LogEvent), but it has less possibilities than the commandline tool EventCreate for XP/2003.

(in reply to willem)
 
 
Post #: 3
 
 Re: Writing to the Eventlog - 5/5/2005 11:20:18 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Check out the Win32_NTEventlogFile class to see if its of any use. I don't do a lot of writing to EventLog using scripts and there is no "correct" way for me. As long as it does what you want it to do with the most efficient way you find possible, that's good enough :)

(in reply to willem)
 
 
Post #: 4
 
 Re: Writing to the Eventlog - 5/6/2005 3:13:35 AM   
  willem

 

Posts: 12
Score: 0
Joined: 4/25/2005
From: France
Status: offline
I checked out 2 classes:
Win32_NTEventlogFile: this is a class which let you manage the event log. You can backup, clear, delete, create etc. a complete EventLog. But not insert any events :(
Win32_NTEventlog: this class is the blueprint for all the log entries. I was very hopeful... but it has no methods. Can just be used for retrieving events. for those who want to query the eventlog (i quit after hunderd records, for if the query is somewhat wide):

Set wbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set wbemObjectSet = wbemServices.InstancesOf("Win32_NTLogEvent")
'*** the time is noted in seconds (see query below:), this gets all events for today from application log:
Set wbemObjectSet = wbemServices.ExecQuery("SELECT * FROM Win32_NTLogEvent WHERE LogFile='Application' AND TimeGenerated > '20050506000000.0+120'")
wscript.echo wbemObjectSet.count
counter=1
For each wbemObject in wbemObjectset
wscript.echo "Message: " & wbemObject.Message & " - Time: " & wbemObject.TimeGenerated
counter=counter+1
If counter > 100 Then wscript.quit
Next

Still not found a way to insert the EventID under windows 2000...

(in reply to willem)
 
 
Post #: 5
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Writing to the Eventlog Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts