【问题标题】:Ping script with email in vbs在 vbs 中使用电子邮件的 Ping 脚本
【发布时间】:2026-02-04 02:25:01
【问题描述】:

我知道我已经问过有关 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 秒到达的。有人可以告诉我如何用标志制作它吗?所以离线时只有一封邮件来?

感谢您的帮助。

【问题讨论】:

    标签: vbscript email wsh


    【解决方案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
    

    【讨论】:

    • 谢谢,我试了一下,但出现了语法错误……请问您能检查一下您的代码吗?对不起,我以前从未使用过标志
    • 尝试修复语法错误。立即查看
    • 现在出现错误消息:Object Required [string: "ON"]。你有什么想法吗?
    最近更新 更多