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

Darren Rose 281 Reputation points
2022-07-09T18:56:00.787+00:00

Hi

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  
  
            Try  
                If My.Computer.Network.Ping(r.ToString, 1) Then  
                    online.Add(r.ToString)  
                Else  
                    offline.Add(r.ToString)  
                End If  
            Catch ex As Exception  
                offline.Add(r.ToString)  
            End Try  
        Next  

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.

VB
VB
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
    2022-07-09T19:25:04.393+00:00

    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
    2022-07-10T09:28:44.7+00:00

    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
    2023-03-23T20:14:34.6633333+00:00

    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