VBSCRIPT - How can I Nslookup/ping a hostname, and store the DNS zone info (.xyz.contsono.com) from the output?

Basicaly I need a way to take a hostname 'exampleserver' and grab it's dns zone information(.xyz.contoso.com or xyz.zy.contoso.com) and store it.


I dont care how it does it, nslookup or ping on the hostname.

Sorry a new to grabbing outputs and storing them. Thanks
WinPEAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi, try this:

'==============
strServer = InputBox("Please enter the server you want to get the FQDN of:", "Server")
If Trim(strServer) <> "" Then
      Set objShell = CreateObject("WScript.Shell")
      Set objExec = objShell.Exec("cmd /c nslookup " & strServer)
      While objExec.Status
            WScript.Sleep 100
      Wend
      strOutput = objExec.StdOut.ReadAll
      arrLines = Split(strOutput, VbCrLf)
      strServer = "NOT FOUND"
      For Each strLine In arrLines
            If Left(strLine, 6) = "Name: " Then strServer = Trim(Mid(strLine, 7))
      Next
      MsgBox strServer
Else
      MsgBox "No server was entered."
End If
'==============

Regards,

Rob.
0
WinPEAuthor Commented:
Thanks Rob, how can I grab the dns zone names only though?

instead of exampleserver.xyz.contoso.com or exampleserver.xyz.zy.contoso.com I need to grab the xyz.contoso.com or xyz.zy.contoso.com values only.

I dont know how a trim would work if the dns zones could always vary in length?
0
RobSampsonCommented:
That's easy, you can use the InStr function to find the first "dot", and the Mid function to get the rest of the text after that dot.

Try this:
'==============
strServer = InputBox("Please enter the server you want to get the FQDN of:", "Server")
If Trim(strServer) <> "" Then
      Set objShell = CreateObject("WScript.Shell")
      Set objExec = objShell.Exec("cmd /c nslookup " & strServer)
      While objExec.Status
            WScript.Sleep 100
      Wend
      strOutput = objExec.StdOut.ReadAll
      arrLines = Split(strOutput, VbCrLf)
      strDNSName = "NOT FOUND"
      For Each strLine In arrLines
            If Left(strLine, 6) = "Name: " Then strDNSName = Trim(Mid(strLine, 7))
      Next
      intPos = InStr(strDNSName, ".")
      If intPos > 0 Then
            strDNSName = Mid(strDNSName, intPos + 1)
      End If
      MsgBox strDNSName
Else
      MsgBox "No server was entered."
End If
'==============

Regards,

Rob.
0
The Ultimate Tool Kit for Technolgy Solution Provi

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 for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

WinPEAuthor Commented:
Awesome, thanks so much Rob. One final question.

How do I change the code so that it has error checking if there is no dns record. Say if a bad hostname is added and no record is found.. I know you have NO FOUND, but I'd like it if there was another loop checking.

Thanks again so much!
0
RobSampsonCommented:
By "another loop checking", do you want it to ask you again to enter another host name?

Rob.
0
WinPEAuthor Commented:
Sorry I know this is tedius, I definitly am learning from it though.

Here's the setup. In the main sub, there is a forloop gathering from an array of computer and running this 'test function' passing one computer at a time.

Below is the current function, basicaly I need to Add what you gave above and a ping test, along side with this wmi test.
Function TestConnection(strComputer)
	On Error Resume Next
	Dim objSWbemLocator, objWMIService, colItems, objItem
		Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	End If
	If (Err <> 0) Then
		objDbrComputers("ErrorNumber") = Err.Number
		objDbrComputers("ErrorDescription") = Err.Description
	    ReportProgress "* Failed to connect to: " & strComputer
	    Err.Clear
	    TestConnection = False
	    Exit Function
	End If
	Set colItems = objWMIService.ExecQuery("Select DomainRole, Name From Win32_ComputerSystem",,48)
	For Each objItem in colItems
		objDbrComputers("ErrorNumber") = 0
		objDbrComputers("ErrorDescription") = "Passed to INV-Server"
		objDbrComputers("DomainRole") = objItem.DomainRole
		objDbrComputers("WMIName") = objItem.Name
	Next
	TestConnection = True
End Function 

Open in new window

0
RobSampsonCommented:
Hmmm, OK, how about this....I have put the code I provided above your TestConnection = True statement, and changed
   MsgBox strDNSName
to
   objDbrComputers("WMIName") = strDNSName

so from what I can tell, you'll have to create the objDbrComputers("WMIName") element in your array.

Regards,

Rob.
0
RobSampsonCommented:
Huh, I forgot to post the code! Sorry:

'============
Function TestConnection(strComputer)
      On Error Resume Next
      Dim objSWbemLocator, objWMIService, colItems, objItem
            Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
      End If
      If (Err <> 0) Then
            objDbrComputers("ErrorNumber") = Err.Number
            objDbrComputers("ErrorDescription") = Err.Description
          ReportProgress "* Failed to connect to: " & strComputer
          Err.Clear
          TestConnection = False
          Exit Function
      End If
      Set colItems = objWMIService.ExecQuery("Select DomainRole, Name From Win32_ComputerSystem",,48)
      For Each objItem in colItems
            objDbrComputers("ErrorNumber") = 0
            objDbrComputers("ErrorDescription") = "Passed to INV-Server"
            objDbrComputers("DomainRole") = objItem.DomainRole
            objDbrComputers("WMIName") = objItem.Name
      Next
      If Trim(strComputer) <> "" Then
            Set objShell = CreateObject("WScript.Shell")
            Set objExec = objShell.Exec("cmd /c nslookup " & strComputer)
            While objExec.Status
                  WScript.Sleep 100
            Wend
            strOutput = objExec.StdOut.ReadAll
            arrLines = Split(strOutput, VbCrLf)
            strDNSName = "<NOT FOUND>"
            For Each strLine In arrLines
                  If Left(strLine, 6) = "Name: " Then strDNSName = Trim(Mid(strLine, 7))
            Next
            intPos = InStr(strDNSName, ".")
            If intPos > 0 Then
                  strDNSName = Mid(strDNSName, intPos + 1)
            End If
            objDbrComputers("WMIName") = strDNSName
      Else
            MsgBox "No server was entered."
      End If

      TestConnection = True
End Function
'============

Regards.

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WinPEAuthor Commented:
Awesome, works like a charm!
0
ADP indiaCommented:
It Even Works for me .
Thanks to experts-exchange team
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.