Print
Willem-Jan Vroom
Category: vbscript
Hits: 188

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:

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)
Next

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 
    Else
        
        Reachable         = False
        set fn_objShell   = createobject("wscript.shell")
	Set fn_objFSO     = CreateObject("Scripting.FileSystemObject")
	strTempFile       = fn_objFSO.GetSpecialFolder(TemporaryFolder) & "\" & fn_objFSO.GetTempName
        fn_objShell.run     "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
	Loop
	ts.Close
        fn_objFSO.DeleteFile strTempFile
    End If
  Next

End Function

  

Attachments:
Download this file (TheAdvancedPing.7z)TheAdvancedPing.7z[ ]0.9 kB