Quickest way to check if list of computers are online or not

Darren Rose 281 Reputation points


I have a list of computer names which i need to loop through and check to see which of them are online (accessible) before I then proceed with the next step in my app. In my powershell script which I am converting to a VB.net app I was using the below which worked quite quickly

Foreach ($ADcomputer in $ADcomputers) {  
    Write-Progress -Activity "Testing connection" -Status $ADcomputer -PercentComplete (($count / $ADcomputers.Count) * 100)  
    If (Test-Connection -ComputerName $ADcomputer -Quiet -Count 1 -ErrorAction SilentlyContinue) {  
        $online += $ADcomputer  
    Else {  
        $offline += $computer  
    $count += 1  

In VB I have tried using the below but it is quite a lot slower

Dim online As New List(Of String)  
        Dim offline As New List(Of String)  
        For Each r In result  
                If My.Computer.Network.Ping(r.ToString, 1) Then  
                End If  
            Catch ex As Exception  
            End Try  

So hence my question what would be the best way to quckly check if a computer is online, these are all domain connected computers, so on reliable quick connections usually, and the list of computers I would be checking is usually around 400 computers.

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,496 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 80,906 Reputation points

    Test-Connection calls WMI with Win32_PingStatus, so you can do the same thing in VB, with request like :

    (remove space in Selec t...)
    "Selec t * from Win32_PingStatus where ((Address='COMPUTERNAME') And TimeToLive=80 And BufferSize=32)"

  2. Castorix31 80,906 Reputation points

    Another method that you can test is by calling IcmpSendEcho directly.

    Cannot post test code here (bug in those forums...) : IcmpSendEcho test in VB
    (probably you should reduce the TimeOut, maybe to a few ms)

    Declarations :

    <DllImport("Iphlpapi.dll", SetLastError:=True, CharSet:=CharSet.Auto)>  
    Public Shared Function IcmpSendEcho(IcmpHandle As IntPtr, DestinationAddress As Integer, RequestData As String,  
          RequestSize As Short, RequestOptions As IntPtr, ByRef ReplyBuffer As ICMP_ECHO_REPLY, ReplySize As UInteger, Timeout As UInteger) As UInteger  
    End Function  
    <DllImport("Iphlpapi.dll", SetLastError:=True, CharSet:=CharSet.Auto)>  
    Public Shared Function IcmpCreateFile() As IntPtr  
    End Function  
    <DllImport("Iphlpapi.dll", SetLastError:=True, CharSet:=CharSet.Auto)>  
    Public Shared Function IcmpCloseHandle(IcmpHandle As IntPtr) As Boolean  
    End Function  
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>  
    Public Structure ICMP_ECHO_REPLY  
        Public Address As Integer  
        Public Status As UInteger  
        Public RoundTripTime As UInteger  
        Public DataSize As UShort  
        Public Reserved As UShort  
        Public Data As IntPtr  
        Public Options As ICMP_OPTIONS  
    End Structure  
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>  
    Public Structure ICMP_OPTIONS  
        Public Ttl As Byte  
        Public Tos As Byte  
        Public Flags As Byte  
        Public OptionsSize As Byte  
        Public OptionsData As IntPtr  
    End Structure  

  3. Michael Quick 0 Reputation points

    You could run Start-Job and collect the results of the checks etc with X number of cores at the same time. That would make your checks exponentially faster.

    0 comments No comments