• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

VB2008 - DNS lookup from IP

Hi Guys,

I initially did this in VBS but then realised I can't run a VBS in the WinPE environment so need to make this an executable.

All of our machines IP's and Hostnames are prestaged based on their Mac address, so mac address 00:00:00:00:00:01 would be given IP and Machine name Computer_1 by default in DNS.

What I need to do is create a small app using VB that will look for the machines name in DNS, this will then allow me to change the answer file on the fly inserting the Machine name in the answer file making sure that all of my machines are named correctly bsed on their DNS entry.

Any help would be really cool thanks guys.

  • 2
1 Solution
pnorris99Author Commented:
Here is the VBS script I created and was going to use, but 2 problems 1 being no NSlookup in the PE env, and 2 you cant run VBS in the PE env.

strcomputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objitem In colitems
   strIPAddress = Join(objitem.IPAddress, ",")

   Exit For

set objShell = createobject("wscript.shell")

strParams = "%comspec% /c NSlookup " & strIPAddress
Set objExecObj = objShell.exec(strParams)

Do While Not objExecObj.StdOut.AtEndOfStream
	strText = objExecObj.StdOut.Readline()
	If instr(strText, "Server") then 
		strServer = trim(replace(strText,"Server:",""))
	Elseif instr (strText, "Name") Then
		strhost = trim(replace(strText,"Name:",""))
	End if

NewString = Right(strhost,6)  

If instr(strhost,".TEST.COM.AU") Then

Artsstring = Replace(strhost, ".TEST.COM.AU", "")

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("X:\sources\wdsunattend\wdsimageunattend.xml", ForReading)

strText = objFile.ReadAll
strNewText = Replace(strText, "$REPLACE$", Artsstring)

Set objFile = objFSO.OpenTextFile("X:\sources\wdsunattend\wdsimageunattend.xml", ForWriting)
objFile.WriteLine strNewText

End If

If instr(strhost,".TEST.COM.AU") Then

Albanystring = Replace(strhost, ".TEST.COM.AU", "")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("X:\Sources\wdsunattend\WdsImageUnattend.xml", ForReading)

strText = objFile.ReadAll
strNewText = Replace(strText, "$REPLACE$", Albanystring)

Set objFile = objFSO.OpenTextFile("X:\Sources\wdsunattend\WdsImageUnattend.xml", ForWriting)
objFile.WriteLine strNewText

End If

Open in new window

Hello Phil,

add this API  for VB6
Private Declare Function GetComputerName Lib "kernel32" _
Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As _
Long) As Long


use th function for getting the machine name
Public Function ComputerName() As String
  Dim sBuffer As String
  Dim lAns As Long
  sBuffer = Space$(255)
  lAns = GetComputerName(sBuffer, 255)
  If lAns <> 0 Then
        'read from beginning of string to null-terminator
        ComputerName = Left$(sBuffer, InStr(sBuffer, Chr(0)) - 1)
        Err.Raise Err.LastDllError, , _
          "A system call returned an error code of " _
           & Err.LastDllError
   End If

End Function
similarly u can use the GetHostByName  API to get the information  for DNS
 try it

still confusion   TRY this site  


you will read example also for that
pnorris99Author Commented:
Thats perfect thanks for your time mate,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now