Sometimes you ping a computer, but due to old local DNS cache, the ‘wrong’ computer replies, even after a ipconfig /flushdns. In this script it is solved by pinging the IP address and resolve it back to its hostname.

A little bit more about the solution:

  • You ping a hostname with – for example:
    ping computername
  • You receive a IP address back, for example
  • With the command:
    ping -a
    You receive a hostname back.
  • You check the returned hostname with the requested hostname and if they match, it is ok. And if they do not match, then the hostname cannot be reached.

The code:


Option Explicit

dim strComputers : strComputers = "dc01,dc02,comp1,comp2,demoats-dc"
dim arrComputers : arrComputers = split(strComputers,",")
dim objComputer

For Each objComputer in ArrComputers
    wscript.echo "Testing: " & objComputer & ". Result: " & Reachable(objComputer)

Function Reachable(strHostName)

  Const ForReading      = 1
  Const TemporaryFolder = 2

  Dim fn_objWMIService
  Dim fn_colPings
  Dim fn_objStatus
  Dim fn_objShell
  Dim fn_objFSO
  Dim strTempFile
  Dim results
  Dim ts
  Dim retString
  strHostName = lcase(strHostName)

  Set fn_objWMIService     = GetObject("winmgmts:\\.\root\cimv2")
  Set fn_colPings          = fn_objWMIService.ExecQuery ("Select * From Win32_PingStatus where Address = '" & strHostName & "'")

  For Each fn_objStatus in fn_colPings
    If IsNull(fn_objStatus.StatusCode) or fn_objStatus.StatusCode <> 0 Then 
        Reachable = False 
        Reachable         = False
        set fn_objShell   = createobject("")
	Set fn_objFSO     = CreateObject("Scripting.FileSystemObject")
	strTempFile       = fn_objFSO.GetSpecialFolder(TemporaryFolder) & "\" & fn_objFSO.GetTempName     "cmd /c ping -a " & fn_objStatus.ProtocolAddress  & " > " & chr(34) & strTempFile & chr(34),0,true      
	Set results       = fn_objFSO.GetFile(strTempFile)
	set ts            = results.OpenAsTextStream(ForReading)
	do While ts.AtEndOfStream <> True
		retString = lcase(ts.ReadLine)
		if instr(retString, strHostName)>0 then
			Reachable = True
			exit do
		end if
        fn_objFSO.DeleteFile strTempFile
    End If

End Function