VBS To Event Log

匿名 (未验证) 提交于 2019-12-03 01:36:02

问题:

I have a script that I am currently using to check when that network goes up or down. Its writing to a pinglog.txt .

For the life of me I can not figure out how to get it to write to the event log when the network goes down. Where it says:

   Call logme(Time & " - "  & machine & " is not responding to ping, CALL FOR      HELP!!!!",strLogFile)  

Thats what I need to write to the Event Log "Machine is not repsonding to ping, CALL FOR HELP!!!!

 'Ping multiple computers and log when one doesn't respond.  '################### Configuration #######################   'Enter the IPs or machine names on the line below separated by a semicolon   strMachines = "4.2.2.2;8.8.8.8;8.8.4.4"    'Make sure that this log file exists, if not, the script will fail.    strLogFile = "c:\logs\pinglog.txt"     '################### End Configuration ###################     'The default application for .vbs is wscript. If you double-click on the script,    'this little routine will capture it, and run it in a command shell with cscript.     If Right(WScript.FullName,Len(WScript.FullName) - Len(WScript.Path)) <>       "\cscript.exe" Then            Set objWMIService = GetObject("winmgmts:    {impersonationLevel=impersonate}!\\.\root\cimv2")         Set objStartup = objWMIService.Get("Win32_ProcessStartup")         Set objConfig = objStartup.SpawnInstance_         Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")         objProcess.Create WScript.Path + "\cscript.exe """ + WScript.ScriptFullName + """", Null, objConfig, intProcessID     WScript.Quit     End If      Const ForAppending = 8     Const ForReading = 1     Const ForWriting = 2      Set objFSO = CreateObject("Scripting.FileSystemObject")      If objFSO.FileExists(strLogFile) Then     Set objFolder = objFSO.GetFile(strLogFile)     Else Wscript.Echo "Log file does not exist. Please create " & strLogFile WScript.Quit   End If   aMachines = Split(strMachines, ";")      Do While True      For Each machine In aMachines             Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._             ExecQuery("select * from Win32_PingStatus where address = '"_             & machine & "'")             For Each objStatus In objPing                        If IsNull(objStatus.StatusCode) Or objStatus.StatusCode<>0 Then                                  Call logme(Time & " - "  & machine & " is not responding to   ping, CALL FOR     HELP!!!!",strLogFile)                         Else                                 WScript.Echo(Time & " + "  & machine & " is responding to       ping, we are good")                         End If                Next         Next         WScript.Sleep 5000   Loop    Sub logme(message,logfile)     Set objTextFile = objFSO.OpenTextFile(logfile, ForAppending, True)     objtextfile.WriteLine(message)     WScript.Echo(message)     objTextFile.Close   End Sub 

Sorry about the spacing in the code. Thanks for the help

回答1:

Use the WshShell object:

object.LogEvent(intType, strMessage [,strTarget])

object WshShell object.

intType Integer value representing the event type.

strMessage String value containing the log entry text.

strTarget Optional. String value indicating the name of the computer system where the event log is stored (the default is the local computer system). Applies to Windows NT/2000 only.

Like so:

Option Explicit  Dim shl  Set shl = CreateObject("WScript.Shell")  Call shl.LogEvent(1,"Some Error Message")  Set shl = Nothing WScript.Quit 

The first argument to LogEvent is an event type:

0    SUCCESS 1    ERROR 2    WARNING 4    INFORMATION 8    AUDIT_SUCCESS 16   AUDIT_FAILURE 

EDIT: more detail

Replace your entire 'logme' sub-routine with this

  Sub logme(t,m)       Dim shl        Set shl = CreateObject("WScript.Shell")        Call shl.LogEvent(t,m)        Set shl = Nothing   End Sub 

Then change this line:

Call logme(Time & " - "  & machine & " is not responding to   ping, CALL FOR HELP!!!!",strLogFile) 

To:

Call logme(1, machine & " is not responding to   ping, CALL FOR HELP!!!!") 


文章来源: VBS To Event Log
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!