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?
 
RobSampsonConnect With a Mentor Commented:
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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
 
WinPEAuthor Commented:
Awesome, works like a charm!
0
 
ADP indiaCommented:
It Even Works for me .
Thanks to experts-exchange team
0
All Courses

From novice to tech pro — start learning today.