views:

49

answers:

1

Hello,

i know i asked already the question about the ping script but now i have a new question about it :-) I hope someone can help me again.

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 = "[email protected]"
            objEmail.To = "[email protected]"
            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

My problem is now, the mail comes all 2 seconds, when the computer are offline. Can someone show me how to make it with flags? So only one mail comes when its offline?

Thanks for your help.

+1  A: 

Use a flag and report only when state changes

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
belisarius
thank you, i try it, but there comes an syntax error...can you check your code, pls? I'm sry i never worked with flags before
matthias
Tried to fix syntax errs. Check it now
belisarius
now it comes the error message: Object Required [string: "ON"]. Have you an idea?
matthias
ok thanks i have the problem, the SET was to mutch...
matthias
syntax corrected
belisarius