本文介绍了VBS 到事件日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个脚本,我目前正在使用它来检查该网络何时启动或关闭.它写入 pinglog.txt .对于我的生活,我无法弄清楚如何在网络出现故障时将其写入事件日志.它说: Call logme(Time & " - " & machine & " is not response to ping, CALL FOR帮助!!!!",strLogFile)这就是我需要写入事件日志的内容机器无法响应 ping,请寻求帮助!!!! 'Ping 多台计算机并在其中一台没有响应时登录.'################### 配置 #######################'在下面的行中输入 IP 或机器名称,以分号分隔strMachines = "4.2.2.2;8.8.8.8;8.8.4.4"'确保此日志文件存在,否则脚本将失败.strLogFile = "c:\logs\pinglog.txt"'################### 结束配置##################'.vbs 的默认应用程序是 wscript.如果双击脚本,'这个小程序将捕获它,并在带有 cscript 的命令 shell 中运行它.If Right(WScript.FullName,Len(WScript.FullName) - Len(WScript.Path)) <>"\cscript.exe" 然后Set objWMIService = GetObject("winmgmts: {impersonationLevel=impersonate}!\\.\root\cimv2")Set objStartup = objWMIService.Get("Win32_ProcessStartup")设置 objConfig = objStartup.SpawnInstance_Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")objProcess.Create WScript.Path + "\cscript.exe """ + WScript.ScriptFullName + """", Null, objConfig, intProcessIDWScript.退出万一Const ForAppending = 8读取常量 = 1写入常量 = 2Set objFSO = CreateObject("Scripting.FileSystemObject")如果 objFSO.FileExists(strLogFile) 那么设置 objFolder = objFSO.GetFile(strLogFile)别的Wscript.Echo "日志文件不存在.请创建" &字符串日志文件WScript.退出万一aMachines = Split(strMachines, ";")做真事对于机器中的每台机器Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._ExecQuery("select * from Win32_PingStatus where address = '"_&机器&"'")对于 objPing 中的每个 objStatus如果 IsNull(objStatus.StatusCode) 或 objStatus.StatusCode0 然后Call logme(Time & " - " & machine & " 没有响应 ping, CALL FOR帮助!!!!",strLogFile)别的WScript.Echo(Time & " + " & machine & " 正在响应 ping,我们很好")万一下一个下一个WScript.Sleep 5000环形子日志(消息,日志文件)设置 objTextFile = objFSO.OpenTextFile(logfile, ForAppending, True)objtextfile.WriteLine(message)WScript.Echo(消息)objTextFile.Close结束子抱歉代码中的间距.感谢您的帮助 解决方案 使用 WshShell 对象:object.LogEvent(intType, strMessage [,strTarget])对象 WshShell 对象.intType 表示事件类型的整数值.strMessage 包含日志条目文本的字符串值.strTarget 可选.指示计算机名称的字符串值存储事件日志的系统(默认为本地计算机系统).仅适用于 Windows NT/2000.像这样:选项显式昏暗Set shl = CreateObject("WScript.Shell")调用 shl.LogEvent(1,"Some Error Message")设置 shl = 无WScript.退出LogEvent 的第一个参数是事件类型:0 成功1 错误2 警告4 信息8 审计_成功16 审核_失败编辑:更多细节用这个替换整个logme"子程序 子 logme(t,m)昏暗Set shl = CreateObject("WScript.Shell")调用 shl.LogEvent(t,m)设置 shl = 无结束子然后改变这一行:Call logme(Time & " - " & machine & " 没有响应ping, CALL FOR HELP!!!!",strLogFile)致:Call logme(1, machine & " 没有响应 ping, CALL FOR HELP!!!")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) ElseWscript.Echo "Log file does not exist. Please create " & strLogFileWScript.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 SubSorry about the spacing in the code. Thanks for the help 解决方案 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 ExplicitDim shlSet shl = CreateObject("WScript.Shell")Call shl.LogEvent(1,"Some Error Message")Set shl = NothingWScript.QuitThe first argument to LogEvent is an event type:0 SUCCESS1 ERROR2 WARNING4 INFORMATION8 AUDIT_SUCCESS16 AUDIT_FAILUREEDIT: more detailReplace 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 SubThen 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 到事件日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-17 02:52