2010-06-16 34 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