使用 VBScript 检查网络连接

问题描述 投票:0回答:3

我正在多台计算机上运行基于网络的幻灯片。我有一个 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
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
© www.soinside.com 2019 - 2024. All rights reserved.