VBScript: Append DNS domain search suffix

04 Nov

The other day, I was asked if I had a script to add domain suffix to a list of remote servers. Well, I had some scripts to add DNS and WINS servers so I thought it will be a quick one to modify those scripts to add domain suffix instead.

A quick search with Mr. google shows an overly simplistic line of code that ask for changing the tcpip registry key using wshshell.regwrite method. Why I call it overly simplistic? Well, if I needed a vbscript, most of the time I would like to do this in a batch to remote servers, no? Wshshell.regwrite method can only be run locally, if I can access the host locally, why not just append the value manually?

Anyway, the method to use is SetDNSSuffixSearchOrder() in the win32_networkadapterconfiguration class. Although document says it only for Windows NT, I tried it with W2k3 and still find it working. Of course, the alternative is the EnableDNS method.

Now, I never noticed the little note that says that you cannot use this with an instance because this method belongs to the class and applies globally. So when I did my “select * from win32_networkadapterconfiguration” to invoke SetDNSSuffixSearchOrder(), I got “invalid method” error from cscript. Only after carefully reading some of the scripts, I realised that I had to use this method with the class object instead.

Anyhow, I searched around and did not find any fully functional and practical script and so I combine the few I found and added some of my stuff for real world usage.

My script checks if the suffix you want to add is already there, if not it will be added. The output is a tab delimited output. If you pipe the output in batch into a file with the cscript //nologo command, you can just copy and paste the text contents into Excel and each column will sit nicely into a cell, where you can sort the results.

set objArg = WScript.Arguments

if objArg.Count >= 2 then 

  strComputer 	= objArg(0)
  strDNSSuffix2Add  = lcase(objArg(1))

  On error resume next

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

  Set colNetaCfg = oWMI.ExecQuery _
      ("Select * From Win32_NetworkAdapterConfiguration" _
       & " where ipEnabled = true")

  On error goto 0

  If err.number = 0 then

    For Each oNetaCfg In colNetaCfg
      strDNSSuffixList = ""

      'some are backup nics without gateway or with private address
    	If not IsNull(oNetaCfg.DefaultIPGateway) then

    		strNetaIndex = trim(CStr(oNetaCfg.Index))
        IsSuffixFound = false
    		If IsArray(oNetaCfg.DNSDomainSuffixSearchOrder) Then
    		  x = UBound(oNetaCfg.DNSDomainSuffixSearchOrder)

    			For iCount = 0 To UBound(oNetaCfg.DNSDomainSuffixSearchOrder)
    				strDNSSuffixList = oNetaCfg.DNSDomainSuffixSearchOrder(x - iCount) & "," & strDNSSuffixList

            if lcase(oNetaCfg.DNSDomainSuffixSearchOrder(x - iCount)) = strDNSSuffix2Add then
    			    IsSuffixFound = true
    			  end if

          if IsSuffixFound then
        	  WScript.Echo strComputer & chr(9) & oNetaCfg.Description & chr(9) & strNetaIndex & chr(9) & "ALREADY THERE"

            strDNSSuffixList = strDNSSuffix2Add & "," & strDNSSuffixList
            strDNSSuffixList = left(strDNSSuffixList, len(strDNSSuffixList)-1)  

            'wscript.echo strDNSSuffixList
            aDNSSuffix = split(strDNSSuffixList, ",")

            'this needs to run at class not instance
            Set oClass = oWMI.Get(oNetaCfg.Path_.class)
            errCode = oClass.SetDNSSuffixSearchOrder(aDNSSuffix)  

        	  WScript.Echo strComputer & chr(9) & oNetaCfg.Description & chr(9) & strNetaIndex & chr(9) & "ADDED SUFFIX"
          end if

      	end if

    	end if



   	WScript.Echo strComputer & chr(9) & err.description

  end if


  WScript.Echo "Syntax: <script> <hostname> <dns suffix to add>"

End If
1 Comment

Posted by on November 4, 2010 in Scripts, VBScripts, Windows


One response to “VBScript: Append DNS domain search suffix

  1. gnome

    April 19, 2011 at 4:08 pm

    I’m new to scripting. Can you help me write a vbscript to obtain DNS Suffix on multiple servers from a textfile (serverlist.txt) and create an output all responses to a single server with the following extenstion (txt, csv, xls) preferably excel file.

    From the script above, where is the source file and the output file? I would like to do this when I start adding the additional suffix on our servers.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: