VBS到事件日志

Ale*_*lex 1 vbscript

我有一个脚本,我目前用来检查网络何时上升或下降.它写入pinglog.txt.

对于我的生活,我无法弄清楚如何在网络出现故障时写入事件日志.在哪里说:

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

   HELP!!!!",strLogFile) 
Run Code Online (Sandbox Code Playgroud)

多数民众赞成我需要写入事件日志"机器没有重新启动ping,请求帮助!!!!

 '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
Run Code Online (Sandbox Code Playgroud)

很抱歉代码中的间距.谢谢您的帮助

Job*_*bbo 9

使用WshShell对象:

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

对象 WshShell对象.

intType表示事件类型的整数值.

strMessage包含日志条目文本的字符串值.

strTarget可选.字符串值,指示存储事件日志的计算机系统的名称(默认为本地计算机系统).仅适用于Windows NT/2000.

像这样:

Option Explicit

Dim shl

Set shl = CreateObject("WScript.Shell")

Call shl.LogEvent(1,"Some Error Message")

Set shl = Nothing
WScript.Quit
Run Code Online (Sandbox Code Playgroud)

LogEvent的第一个参数是一个事件类型:

0    SUCCESS
1    ERROR
2    WARNING
4    INFORMATION
8    AUDIT_SUCCESS
16   AUDIT_FAILURE
Run Code Online (Sandbox Code Playgroud)

编辑:更多细节

用这个替换整个'logme'子程序

  Sub logme(t,m)
      Dim shl

      Set shl = CreateObject("WScript.Shell")

      Call shl.LogEvent(t,m)

      Set shl = Nothing
  End Sub
Run Code Online (Sandbox Code Playgroud)

然后改变这一行:

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

至:

Call logme(1, machine & " is not responding to   ping, CALL FOR HELP!!!!")
Run Code Online (Sandbox Code Playgroud)