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)"
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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)"
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
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.