2010-06-16 85 views
0

我知道我已经问过关于ping脚本的问题,但现在我有一个关于它的新问题:-)我希望有人可以再次帮助我。用vbs电子邮件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 = "[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 

我的问题是,现在的邮件都来了2秒,当计算机处于脱机状态。有人能告诉我如何使用标志吗?所以当它离线时只有一封邮件来了?

感谢您的帮助。

回答

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 
+0

谢谢你,我尝试,但总会有一个语法错误...你可以检查你的代码,请?我是sry,我从来没有使用过标志 – Sebastian 2010-06-16 13:55:59

+0

试图修复语法错误。现在检查 – 2010-06-16 14:26:17

+0

现在它出现错误消息:Object Required [string:“ON”]。你有想法吗? – Sebastian 2010-06-17 07:09:58