【问题标题】:Check network connection with VBScript使用 VBScript 检查网络连接
【发布时间】:2015-06-24 13:45:29
【问题描述】:

我正在多台计算机上运行基于网络的幻灯片。我有一个在启动时运行的 VBScript,打开 IE 并以全屏模式导航到特定页面。只要启动时有互联网连接,一切都很好。如果没有,则页面永远不会加载。 VBScript 中有没有办法每隔几分钟检查一次连接,直到找到连接,然后继续执行脚本?以下是供您参考的代码:

Option Explicit     
Dim WshShell
set WshShell = WScript.CreateObject("WScript.Shell")         

On Error Resume Next
   With WScript.CreateObject ("InternetExplorer.Application")     
      .Navigate "http://www.example.com/slideshow"
      .fullscreen = 1   
      .Visible    = 1
      WScript.Sleep 10000
   End With    
On Error Goto 0

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    参考这个 ==> Loop a function?

    是的,您可以使用以下代码轻松完成:

    Option Explicit
    Dim MyLoop,strComputer,objPing,objStatus
    MyLoop = True
    While MyLoop = True
        strComputer = "smtp.gmail.com"
        Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery _
        ("select * from Win32_PingStatus where address = '" & strComputer & "'")
        For Each objStatus in objPing
            If objStatus.Statuscode = 0 Then
                MyLoop = False
                Call MyProgram()
                wscript.quit
            End If
        Next
        Pause(10) 'To sleep for 10 secondes
    Wend
    '**********************************************************************************************
     Sub Pause(NSeconds)
        Wscript.Sleep(NSeconds*1000)
     End Sub
    '**********************************************************************************************
    Sub MyProgram()
    Dim WshShell
    set WshShell = WScript.CreateObject("WScript.Shell")         
    On Error Resume Next
       With WScript.CreateObject ("InternetExplorer.Application")     
          .Navigate "http://www.example.com/slideshow"
          .fullscreen = 1   
          .Visible    = 1
          WScript.Sleep 10000
       End With    
    On Error Goto 0
    End Sub
    '**********************************************************************************************
    

    【讨论】:

      【解决方案2】:

      如果 Hackoo 的代码对您不起作用,您可以尝试以下方法。并非所有服务器都会响应 ping 请求,但您可以发出 HTTP 请求并查看服务器是否发送有效响应(状态 = 200)。

      Function IsSiteReady(strURL)
      
          With CreateObject("MSXML2.XMLHTTP")
              .Open "GET", strURL, False
              .SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"
              On Error Resume Next
              .Send
              If .Status = 200 Then IsSiteReady = True
          End With
      
      End Function
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-13
        • 2010-10-05
        相关资源
        最近更新 更多