Quickest way to determine if a remote PC is online

岁酱吖の 提交于 2019-12-24 17:42:37

问题


I tend to run a lot of commands against remote PCs, but I always check to make sure they are online first. I currently use this code:

If objFSO.FolderExists("\\" & strHost & "\c$") Then
    'The PC is online so do your thing

However, when the remote PC is not online, it takes my laptop ~45 seconds before it times out and resolves to FALSE.

Is there a way to hasten the timeout? Or is there another easily implementable solution to determine if a PC is online?


回答1:


You could use WMI to ping it.

Function IsOnline(strHost As String) As Boolean

    Dim strQuery As String
    strQuery = "select * from Win32_PingStatus where Address = '" & strHost & "'"

    Dim colItems As Object
    Set colItems = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)

    Dim objItem As Object
    For Each objItem In colItems
        If IsObject(objItem) Then
            If objItem.StatusCode = 0 Then
                IsOnline = True
                Exit Function
            End If
        End If
    Next

End Function



回答2:


You can use the return code from a ping request:

Function HostIsOnline(hostname)
    Set oShell     = WScript.CreateObject("WScript.Shell")
    Set oShellExec = oShell.Exec("ping -n 1 " & hostname)

    While oShellExec.Status = 0
        WScript.Sleep(100)
    Wend

    HostIsOnline = (oShellExec.ExitCode = 0)
End Function


来源:https://stackoverflow.com/questions/31680992/quickest-way-to-determine-if-a-remote-pc-is-online

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!