Script to grab DNS server list from server NICs

Discussion in 'Scripting & Programming' started by zebulebu, Jul 26, 2010.

  1. zebulebu

    zebulebu Terabyte Poster

    3,748
    330
    187
    Any scriptheads help me out with this? I need to pull info about DNS configs (specifically, the nameservers configured on server NICs). I've got the meat of the script (pinched from an MS site) here:

    Code:
    On Error Resume Next
     
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colNicConfigs = objWMIService.ExecQuery _
     ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
     
    For Each objNicConfig In colNicConfigs
      strDNSSuffixSO = ""
      strDNSServerSO = ""
      strDNSHostName = objNicConfig.DNSHostName
      strIndex = objNicConfig.Index
      strDescription = objNicConfig.Description
      strDNSDomain = objNicConfig.DNSDomain
      strDNSSuffixSO = ""
      If Not IsNull(objNicConfig.DNSDomainSuffixSearchOrder) Then
        For Each strDNSSuffix In objNicConfig.DNSDomainSuffixSearchOrder
          strDNSSuffixSO = strDNSSuffixSO & VbCrLf & String(37, " ") & _
     strDNSSuffix
        Next
      End If
      strDNSServerSO = ""
      If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
        For Each strDNSServer In objNicConfig.DNSServerSearchOrder
          strDNSServerSO = strDNSServerSO & VbCrLf & String(37, " ") & _
     strDNSServer
        Next
      End If
      strDomainDNSRegistrationEnabled = _
     objNicConfig.DomainDNSRegistrationEnabled
      strFullDNSRegistrationEnabled = objNicConfig.FullDNSRegistrationEnabled
      strDNSSettings = strDNSSettings & VbCrLf & VbCrLf & _
       "  Network Adapter " & strIndex & VbCrLf & _
       "    " & strDescription & VbCrLf & VbCrLf & _
       "    DNS Domain:                      " & strDNSDomain & VbCrLf & _
       "    DNS Domain Suffix Search Order:" & strDNSSuffixSO & VbCrLf & _
       "    DNS Server Search Order:" & strDNSServerSO & VbCrLf & _
       "    Domain DNS Registration Enabled: " & _
       strDomainDNSRegistrationEnabled & VbCrLf & _
       "    Full DNS Registration Enabled:   " & _
       strFullDNSRegistrationEnabled
    Next
     
    WScript.Echo VbCrLf & "DNS Settings" & VbCrLf & VbCrLf & _
     "Host Name: " & strDNSHostName & strDNSSettings
    The actual script works a treat, but I'm having trouble feeding it a bunch of server names from a text file then exporting it to a results csv/text file. Ordinarily, I'd feed it in via a batch file using this:

    Code:
    @Echo Off
    Setlocal Enableextensions
    For /f %%c In ('serverlist.txt') Do (
      Cscript //Nologo insertscripthere.vbs %%c
    )
    Endlocal
    However, there's something screwy with the logic of the script but, not being a scripting guy I can't see where it is. Anyone got any quick ideas?
     
    Certifications: A few
    WIP: None - f*** 'em
  2. greenbrucelee
    Highly Decorated Member Award

    greenbrucelee Zettabyte Poster

    14,292
    265
    329
    see if this helps http://software.allfaq.org/forums/t/179062.aspx
     
    Certifications: A+, N+, MCDST, Security+, 70-270
    WIP: 70-620 or 70-680?
  3. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    Something like this?

    Code:
    Dim ArgObj, cname
    Set ArgObj = WScript.Arguments
    Set WshShell = WScript.CreateObject("WScript.Shell")
    
    cname = ArgObj(0)
     
    strComputer = cname
    
    it would allow you to call it like "testscript.vbs computername" or if you wanted to pass /computername then you would just need to strip the / at the strComputer veriable
     
    Last edited: Jul 26, 2010
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)
  4. ChrisH1979

    ChrisH1979 Byte Poster

    225
    9
    37
    I added a few bits to you script to read from a file and output the results to a file. I basically copied and pasted from some of my other scripts so it is untested. I may need a few tweaks to get it running.

    Code:
    
    const ForReading = 1
    const ForAppending = 8
    inputfile="servername.csv"
    outputfile="results.txt"
    set fso = createobject("scripting.filesystemobject")
    set InputFile = fso.opentextfile(inputfile,forreading)
    set OutputLog = fso.OpenTextFile(outputfile, ForAppending)
    
    
    On Error Resume Next
    Do Until InputFile.atendofstream
    ResultName =""
    CompName=InputFile.readline
    
    strComputer = CompName
    Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colNicConfigs = objWMIService.ExecQuery _
     ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
     
    For Each objNicConfig In colNicConfigs
      strDNSSuffixSO = ""
      strDNSServerSO = ""
      strDNSHostName = objNicConfig.DNSHostName
      strIndex = objNicConfig.Index
      strDescription = objNicConfig.Description
      strDNSDomain = objNicConfig.DNSDomain
      strDNSSuffixSO = ""
      If Not IsNull(objNicConfig.DNSDomainSuffixSearchOrder) Then
        For Each strDNSSuffix In objNicConfig.DNSDomainSuffixSearchOrder
          strDNSSuffixSO = strDNSSuffixSO & VbCrLf & String(37, " ") & _
     strDNSSuffix
        Next
      End If
      strDNSServerSO = ""
      If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
        For Each strDNSServer In objNicConfig.DNSServerSearchOrder
          strDNSServerSO = strDNSServerSO & VbCrLf & String(37, " ") & _
     strDNSServer
        Next
      End If
      strDomainDNSRegistrationEnabled = _
     objNicConfig.DomainDNSRegistrationEnabled
      strFullDNSRegistrationEnabled = objNicConfig.FullDNSRegistrationEnabled
      strDNSSettings = strDNSSettings & VbCrLf & VbCrLf & _
       "  Network Adapter " & strIndex & VbCrLf & _
       "    " & strDescription & VbCrLf & VbCrLf & _
       "    DNS Domain:                      " & strDNSDomain & VbCrLf & _
       "    DNS Domain Suffix Search Order:" & strDNSSuffixSO & VbCrLf & _
       "    DNS Server Search Order:" & strDNSServerSO & VbCrLf & _
       "    Domain DNS Registration Enabled: " & _
       strDomainDNSRegistrationEnabled & VbCrLf & _
       "    Full DNS Registration Enabled:   " & _
       strFullDNSRegistrationEnabled
    Next
     
    'WScript.Echo VbCrLf & "DNS Settings" & VbCrLf & VbCrLf & _
    ResultName ="Host Name: " & strDNSHostName & strDNSSettings
    
    
    
    'To read a whole file ObjFile.ReadAll
    OutputLog.writeline username
    Loop
     
    Certifications: MCITP:SA, MCSA, MCTS:Win 7, Application Infrastructure
    WIP: MCITP:EA
  5. LukeP

    LukeP Gigabyte Poster

    1,194
    41
    90
    Did some research on the net and rewritten the following for you (couldn't find working example):

    Code:
    Get-Content .\servers.txt | `
    
    ForEach-Object {
    
    Get-WMIObject Win32_NetworkAdapterConfiguration -Computername $_ | `
    
    Where-Object {$_.IPEnabled -match "True"} | `
    
    Select-Object -property DNSHostName,@{N="DNSServerSearchOrder";E={"$($_.DNSServerSearchOrder)"}},@{N='IPAddress';E={$_.IPAddress}}
    
    } | Export-CSV -path servers.csv -NoTypeInformation
    It's PowerShell though, but I had a quick look at your VBS and I'm could look into it to make it work.
    Hopefully PowerShell is enough.

    Let me know if you want to export other properties as well. For now columns are Hostname, DNS Servers, IP Address (in case you want to query multiple interfaces and want to know which are which).

    Hope that helps

    Edit: BTW: As you can see takes servers.txt (tested - each server new line and returns CSV). Also just noticed above post so disregard VBS bit (bit lenghty eh...)
     
    Last edited: Jul 26, 2010
    WIP: Uhmm... not sure
  6. zebulebu

    zebulebu Terabyte Poster

    3,748
    330
    187
    Perfect Luke, thanks. I only need to script the DNS entries, so no need for anything else, this works perfectly.

    Cheers!
     
    Certifications: A few
    WIP: None - f*** 'em

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.