.vbs检查一个日志并写入另一个日志

时间:2013-12-24 19:37:52

标签: logging vbscript

我的路由器设置为每天0800的WoL 2系统,除非有时它没有......

为了解决这个问题,我编写了一个脚本在我的服务器上运行(总是打开)来ping所说的系统,如果它们停下来就把它们留下来并留下一个日志文件。

我想知道这是什么时候发生的,所以我正在尝试编写一个脚本来监控WoL日志,按小时计划在上述2个系统+2台笔记本电脑上运行。

我希望这个脚本写入它自己的日志,所以一旦确认一次(在任何系统上,它都不会再显示弹出窗口。

仅供参考,这是服务器WoL脚本,它正在运行。 WoLSystemIfDown.vbs

Const ForAppending = 8

Dim outLogFile
Dim strMserverAlive, strWorkstationAlive, strText

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set WshShell = WScript.CreateObject("WScript.Shell")
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

Function PingM(strMServer)

Dim objExecObject

Set objExecObject = WSHShell.Exec _
    ("%comspec% /c ping -n 3 -w 1000 24.12.46.11")

    Do While Not objExecObject.StdOut.AtEndOfStream
        strText = objExecObject.StdOut.ReadAll()
        If Instr(strText, "Reply") > 0 Then
        ' do nothing
        Else
            set shell=createobject("wscript.shell")
            shell.Run "wolmserver.bat"
            objFile.WriteLine
            objFile.Write Date & " - Media Server was dead when pinged @ " & Time & ", WoL Command was sent from " & strComputername
        End If 
    Loop

End Function

Function PingW(strWorkstation)

Set objExecObject = WSHShell.Exec _
    ("%comspec% /c ping -n 3 -w 1000 24.12.46.12")

    Do While Not objExecObject.StdOut.AtEndOfStream
        strText = objExecObject.StdOut.ReadAll()
        If Instr(strText, "Reply") > 0 Then
        ' do nothing
        Else
            set shell=createobject("wscript.shell")
            shell.Run "wolworkstation.bat"
            objFile.WriteLine
            objFile.Write Date & " - Workstation was dead when pinged @ " & Time & ", WoL Command was sent from "  & strComputername
        End If 
    Loop

End Function

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("ServerWoLLog.txt", ForAppending)


PingM(strMServer)

PingW(strWorkstation)

此代码似乎工作正常,但您可以看到我已经两次注释“无所事事”。它不适用于此处或其他任何内容。

此脚本的日志输出如下所示

21/12/2013 - Workstation was dead when pinged @ 20:02:38, WoL Command was sent from SERVER
23/12/2013 - Workstation was dead when pinged @ 20:02:38, WoL Command was sent from SERVER
23/12/2013 - Media Server was dead when pinged @ 20:35:54, WoL Command was sent from SERVER
24/12/2013 - Media Server was dead when pinged @ 15:35:54, WoL Command was sent from SERVER
24/12/2013 - Workstation was dead when pinged @ 17:07:19, WoL Command was sent from N145

正如我所说,我想知道路由器何时无法使用WoL这些系统,但不是一整天都重复,所以我希望这个脚本检查它是否已经被确认,然后显示一个弹出窗口通知我哪个系统无法唤醒。

Const ForAppending = 8
Const ForReading = 1

Dim objFSO,objFile,objOutFile
Dim strLine, strMsgDisplayed, strMserver, strWstation, strToday

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutFile = objFSO.OpenTextFile("MsgLog.txt", ForReading)

Do Until objOutFile.AtEndOfStream
    strLine = objOutFile.ReadLine
    strMsgDisplayed = Instr(strLine,(date))
Loop

    if strMsgDisplayed Then
    wscript.quit
    End if

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutFile = objFSO.OpenTextFile("MsgLog.txt", ForAppending)
Set objFile = objFSO.OpenTextFile("WoLLog.txt", ForReading)


Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    strToday = Instr(strLine,(date))

Loop

If strToday Then
    Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    strMserver = Instr(strLine, "Media Server")
    strWstation = Instr(strLine, "Workstation")
    Loop
End If


    if strMserver Then
    Wscript.Echo "DD-WRT Router Failed to WoL Media Server Today:" & Date & vbNewLine _
    & "" & vbNewLine _
    & "WoL command was sent from Server Instead. Check ServerWolLog.txt for more details"
    objOutFile.WriteLine
    objOutFile.Write Date
    if strWstation Then
    Wscript.Echo "DD-WRT Router Failed to WoL Workstation Today:" & Date & vbNewLine _
    & "" & vbNewLine _
    & "WoL command was sent from Server Instead. Check ServerWolLog.txt for more details"
    objOutFile.WriteLine
    objOutFile.Write Date
    End if
    End if

Wscript.Quit

我非常感谢任何人花时间阅读这篇文章,我们非常欢迎任何帮助或建议。

圣诞快乐每个人!

1 个答案:

答案 0 :(得分:0)

得到了!!这种方式更有价值。

我去了

Const ForAppending = 8
Const ForReading = 1

Dim objFSO, objFile, objOutFile
Dim strLine, strMsgDisplayed, strMserver, strWstation, strComputer
Set WshShell = WScript.CreateObject("WScript.Shell")

strComputer = "24.12.46.10"

Public Function IsAlive(strComputer)

Dim objPing, objStatus
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
    ExecQuery("select * from Win32_PingStatus where address = '"_
        & strComputer& "'")

For Each objStatus in objPing
    If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then 
        Wscript.Quit
    Else
        Exit Function
    End If

    Next
End Function

IsAlive(strComputer)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutFile = objFSO.OpenTextFile("//24.12.46.10/c\CheckWoLStatus\MsgLog.txt", ForReading)

Do Until objOutFile.AtEndOfStream
    strLine = objOutFile.ReadLine
    strMsgDisplayed = Instr(strLine,(date))
Loop

    if strMsgDisplayed Then
    wscript.quit
    End if

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutFile = objFSO.OpenTextFile("//24.12.46.10/c\CheckWoLStatus\MsgLog.txt", ForAppending)
Set objFile = objFSO.OpenTextFile("//24.12.46.10/c\CheckWoLStatus\ServerWoLLog.txt", ForReading)

strText = objFile.ReadAll

    If Instr(strText, date & " - Media Server") Then
    Wscript.Echo "DD-WRT Router Failed to WoL Media Server Today: " & Date & vbNewLine _
    & "" & vbNewLine _
    & "WoL command was sent from Server Instead. Check ServerWolLog.txt for more details"
    objOutFile.WriteLine
    objOutFile.WriteLine date
    If Instr(strText, date & " - Workstation") Then
    Wscript.Echo "DD-WRT Router Failed to WoL Workstation Today: " & Date & vbNewLine _
    & "" & vbNewLine _
    & "WoL command was sent from Server Instead. Check ServerWolLog.txt for more details"
    objOutFile.WriteLine
    objOutFile.WriteLine date
    End If
    End If


Wscript.Quit

如果笔记本电脑不在我的家庭网络上,那么首先ping服务器还有额外的好处......

希望这对某些人来说很有用!

祝大家圣诞快乐,祝大家晚安...