使用VBScript检查网络连接

5

我正在多台计算机上运行基于Web的幻灯片演示。我有一个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
3个回答

3
请参考此链接 ==> 如何循环执行一个函数? 是的,您可以使用以下代码轻松实现:
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

0
这是一个非常简短的代码,用于检查您的互联网连接:
Function isConnectionON()
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Dim isInternetConnected
    isInternetConnected = 0
    Set oShell = WScript.CreateObject("WScript.Shell")
    strHost = "google.com"
    strPingCommand = "ping -n 1 -w 300 " & strHost
    ReturnCode = oShell.Run(strPingCommand, 0 , True)
    If ReturnCode = 0 Then
        isInternetConnected= 1
    Else
        isInternetConnected= 0
    End If
        isConnectionON=isInternetConnected
End Function

调用这个函数:
isInternetConneted = isConnectionON()
WScript.Echo "isInternetConneted: "&isInternetConneted

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接