Changing Static DNS Search List Using a VBS login Script

This script is fuctional, but rare occurance of no DNS servers has happen when either the delete takes longer then expected or something happens at login that inerupts the completion of the login script. This is why I added a pre-check to see if the change is even needed in the first place.

The only Values needed to be changed are arrNewDNSServerSearchOrder which is the list of new DNS server you wish to add, strOneOfTheOldDNSServers which is a common DNS server already used that you may wish to change or add to, and strOneOfTheNewDNSServers which is one of the new DNS servers you plan to add so the script does not need to fully run at every login (only when change is needed).

If there are any ideas for fixing the rare “No DNS issue” or a better way to do this in PowerShell, please comment. Otherwise enjoy.

'==============================================================
'Script to update DNS settings (Such as adding more then one DNS server to the targeted NIC)
'
'By Paul - https://networksavy.wordpress.com
'Date: 08/23/2010
'==============================================================

'====================================================
'New List of DNS Servers to Apply
'====================================================
arrNewDNSServerSearchOrder = Array( "192.168.1.2", _
                                    "192.168.2.2", _
                                    "192.168.3.2"  )
                                    
'to find the right adapter(s) to change:
' This is the value that checks the right nic (A common DNS server that is already used)
strOneOfTheOldDNSServers = "192.168.2.19"
' This is to check if a value is already correct (So the replacement does not run again if not needed)
strOneOfTheNewDNSServers = "192.168.1.19"


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


Set colNetCards = objWMIService.ExecQuery _
    ("Select DNSServerSearchOrder From Win32_NetworkAdapterConfiguration")

'============================================
'
' Test if the script even needs to be applied
' If the new DNS server exists then the script quits
'
'============================================
For Each objNetCard in colNetCards
  blnFound = 0
  arrDNSServerSearchOrder = objNetCard.DNSServerSearchOrder
  	If Not IsNull(arrDNSServerSearchOrder) Then
    	For i = 0 to UBound(arrDNSServerSearchOrder)
      		If (arrDNSServerSearchOrder(i) = strOneOfTheNewDNSServers) Then
				WScript.Quit
        Exit For
      End If
    Next    
  End If
Next

'============================================
'
' Replaces DNS Servers with new servers
'
'============================================
For Each objNetCard in colNetCards
  blnFound = 0
  arrDNSServerSearchOrder = objNetCard.DNSServerSearchOrder
  If Not IsNull(arrDNSServerSearchOrder) Then
    For i = 0 to UBound(arrDNSServerSearchOrder)
      If (arrDNSServerSearchOrder(i) = strOneOfTheOldDNSServers) Then
        arrDNSServerSearchOrder = Array()
        objNetCard.SetDNSServerSearchOrder(arrDNSServerSearchOrder)
        wscript.sleep 600
        arrDNSServerSearchOrder = arrNewDNSServerSearchOrder
        objNetCard.SetDNSServerSearchOrder(arrDNSServerSearchOrder)
        Exit For
      End If
    Next    
  End If
Next

WScript.Quit
Advertisements

About Paul Cardelli, CISSP
Cyber Security Analyst, and computer guru

3 Responses to Changing Static DNS Search List Using a VBS login Script

  1. Jonathan Rudge says:

    Thanks for this script. Just saved me a lot of work. Does exactly what I was after perfectly.

  2. Steve says:

    Script works great on my Windows 7 x64 machines. It doesn’t work so well on my Server 08 R2 any recommendations?

  3. I know this post is old, but has anyone tested on Windows 7 in a 2008 domain?

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: