6

I want to write event log when this vbscript run. How to i can?

Thanks for any help.

Emree
  • 85
  • 1
  • 1
  • 5

3 Answers3

11

Like so:

Set shell = CreateObject("WScript.Shell")
shell.LogEvent 4, "Your Message Here"

The 4 is a severity level. You can learn more about the LogEvent method on MSDN.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • Thank you for helping. Where write log ? I don't find it – Emree Sep 23 '11 at 20:37
  • @Emree - It should write it to the event viewer. You can find that by running `compmgmt.msc` from the run dialog. Take a look around Google for the the event viewer. – vcsjones Sep 23 '11 at 21:41
1

This is old but I'm sure still valid.

http://msdn.microsoft.com/en-us/library/b4ce6by3

You also need permissions to be able to write to the event log so depending on the user running the script you may or my not have access.

Gratzy
  • 9,164
  • 4
  • 30
  • 45
1

You might want to simply write to your own log file.

Check out my link with more information and details

http://www.yeshaib.com/2010/08/vbscript-in-the-logging/

'----------------------------------------------------------------------
'
' Please Enter Updates with date and name including line of Change
'----------------------------------------------------------------------
'---------------------------------------------------------------------- 

 set objShell = CreateObject("Wscript.Shell")
 set objFSO = CreateObject("Scripting.FileSystemObject")

'--- Main Begins ---------------------------------------

 WriteToLog("Generic Log.vbs - Write This")

'--- Main Ends -----------------------------------------

'--- Write to log --------------------------------------
Sub WriteToLog(strLogMessage)
 Const ForAppending = 8
 Const vbsName = "Generic Log"

 strLogFileName = "C:\GenericLog.log"
 strLogEntryTime = NOW

 'test whether file exists To either write/append to file
 if objFSO.FileExists(strLogFileName) Then
 Set objLogFileTransaction = objFSO.OpenTextFile(strLogFileName, ForAppending)
 Else
 Set objLogFileTransaction = objFSO.CreateTextFile(strLogFileName)
 End if

 objLogFileTransaction.WriteLine strLogEntryTime & chr(9) & chr(58) & chr(9) & vbsName & chr(9) & chr(58) & chr(9) & strLogMessage
 objLogFileTransaction.Close
 WScript.StdOut.WriteLine strLogMessage
 WScript.StdOut.WriteLine ""
End Sub
YeshaiB
  • 135
  • 1
  • 2
  • 8