Vbscript VBS到事件日志
我有一个脚本,我目前正在使用它来检查网络何时启动或关闭。它正在写入pinglog.txt 就我的一生而言,我不知道当网络瘫痪时如何让它写入事件日志。上面写着:Vbscript VBS到事件日志,vbscript,Vbscript,我有一个脚本,我目前正在使用它来检查网络何时启动或关闭。它正在写入pinglog.txt 就我的一生而言,我不知道当网络瘫痪时如何让它写入事件日志。上面写着: Call logme(Time & " - " & machine & " is not responding to ping, CALL FOR HELP!!!!",strLogFile) 这就是我需要写的,事件日志机器不需要再重复ping,呼叫帮助 'Ping multiple comp
Call logme(Time & " - " & machine & " is not responding to ping, CALL FOR
HELP!!!!",strLogFile)
这就是我需要写的,事件日志机器不需要再重复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
很抱歉代码中的空格。感谢您的帮助使用WshShell对象: object.LogEventintType,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
LogEvent的第一个参数是事件类型:
编辑:更多细节
将整个“logme”子例程替换为此
Sub logme(t,m)
Dim shl
Set shl = CreateObject("WScript.Shell")
Call shl.LogEvent(t,m)
Set shl = Nothing
End Sub
然后更改此行:
Call logme(Time & " - " & machine & " is not responding to ping, CALL FOR HELP!!!!",strLogFile)
致:
使用WshShell对象: object.LogEventintType,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
LogEvent的第一个参数是事件类型:
编辑:更多细节
将整个“logme”子例程替换为此
Sub logme(t,m)
Dim shl
Set shl = CreateObject("WScript.Shell")
Call shl.LogEvent(t,m)
Set shl = Nothing
End Sub
然后更改此行:
Call logme(Time & " - " & machine & " is not responding to ping, CALL FOR HELP!!!!",strLogFile)
致:
我试过了,每次都收到一条错误信息。你收到了什么错误信息?这可能表明权限问题我正试图将其添加到此处调用logmeTime&&machine&未响应ping,呼叫帮助!!!!,strLogFile,我在第45行得到它,这是另一个SO链接。选项显式似乎有帮助。对我没有帮助我试过了,每次都收到一条错误消息。你收到了什么错误消息?这可能表明权限问题我正试图将其添加到此处调用logmeTime&&machine&未响应ping,呼叫帮助!!!!,strLogFile,我在第45行得到它,这是另一个SO链接。选项显式似乎有帮助。我如何将其添加到我已有的代码中。抱歉,我对VBS一点也不熟悉。我收到一个错误,显示第57行字符11错误预期标识符代码800A03F2源Microsoft VBS编译错误抱歉,类型是保留字。。。我再次编辑并将类型、消息更改为t,我如何将其添加到我已有的代码中。抱歉,我对VBS一点也不熟悉。我收到一个错误,显示第57行字符11错误预期标识符代码800A03F2源Microsoft VBS编译错误抱歉,类型是保留字。。。我再次编辑并将类型、消息更改为t、m