使用vbs中的电子邮件ping脚本

时间:2010-06-16 12:39:13

标签: vbscript email wsh

我知道我已经问过关于ping脚本的问题,但现在我有一个关于它的新问题:-)我希望有人可以再次帮助我。

strText = "here comes the mail message"

strFile = "test.log"

PingForever strHost, strFile

Sub PingForever(strHost, outputfile)
    Dim Output, Shell, strCommand, ReturnCode

    Set Output = CreateObject("Scripting.FileSystemObject").OpenTextFile(outputfile, 8, True)
    Set Shell = CreateObject("wscript.shell")
    strCommand = "ping -n 1 -w 300 " & strHost
    While(True)
        ReturnCode = Shell.Run(strCommand, 0, True)     
        If ReturnCode = 0 Then
            Output.WriteLine Date() & " - " & Time & " | " & strHost & " - ONLINE"
        Else
            Output.WriteLine Date() & " - " & Time & " | " & strHost & " - OFFLINE"

            Set objEmail = CreateObject("CDO.Message")
            objEmail.From = "noreply@test.net"
            objEmail.To = "test@test.net"
            objEmail.Subject = "Computer" & strHost & " is offline" 
            objEmail.Textbody = strText
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                    "smtpadress" 
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            objEmail.Configuration.Fields.Update
            objEmail.Send

        End If
        Wscript.Sleep 2000
    Wend
End Sub

现在我的问题是,当计算机处于脱机状态时,邮件会全部显示2秒钟。有人能告诉我如何用旗帜制作它吗?所以只有一封邮件在离线时出现?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用标记并仅在状态更改时报告

FLAG0 = "ON"
While(True)
    ReturnCode = Shell.Run(strCommand, 0, True)     
    If ReturnCode = 0 Then
        Output.WriteLine Date() & " - " & Time & " | " & strHost & " - ONLINE"
        FLAG0 = "ON"
    Else
        Output.WriteLine Date() & " - " & Time & " | " & strHost & " - OFFLINE"
        IF FLAG0 = "ON" THEN
           FLAG0 = "OFF"
           Set objEmail = CreateObject("CDO.Message")
           ...... rest of mailing code
        END IF

    End If