Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with VB Script for workstation DNS... 1

Status
Not open for further replies.

blaster99

MIS
Aug 11, 2004
17
US
I am trying to build a script to get and save in a variable the current DNS setting of a non-DHCP enable client. Once I have gotten this variable I would then need to place it back into client with the addition of a current and correct DNS entry. I need to keep the old entrys because they do not know if they are there for a reason or not. I have been able to build the part that puts the entry back, but it over writes the current entries. I am having difficulties trying to get the old DNS entries into a variable to use later. I am relatively new to scripting and have only made simple scripts or just modified ones to suit my needs. Any and all help would be greatly appreciated. Scripts are as follows:

DNS Entry Insertion script:

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = TRUE")
For Each objNetCard in colNetCards
If objNetCard.DHCPEnabled = True Then
object.Skip(3)
Else
arrDNSServers = Array("***.***.***.***", "***.***.***.***")
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
End If
Next

I have been able to get the current DNS entries to echo to the screen, but have no idea how to get them into a variable. Here is what I have for echoing them to the screen:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
n = 1
For Each objAdapter in colAdapters
If Not IsNull(objAdapter.DNSServerSearchOrder) Then
For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
WScript.Echo " " & objAdapter.DNSServerSearchOrder(i)
Next
End If
Next

Thank you in Advance to anyone who can help on this matter.

Sean P. Mehrlander
 
Hello blaster99,

Suppose you want to append n new entries to the dnsserversearchorder list, appending them at the tail or lower order. Also, suppose the append list is the same for different nic's with .dhcpendabled=false.

This is how you get it in your script.
Code:
dim arrDNSServers()
'Your new append list
aNewEntries=array("123.456.789.012","456.789.012.345")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
'n = 1    'not used, reactivate it if you need.
For Each objAdapter in colAdapters
   idim=0
   If Not IsNull(objAdapter.DNSServerSearchOrder) Then
      idim=ubound(objAdapter.DNSServerSearchOrder)
      redim arrDNSServers(idim)
      For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
         WScript.Echo "      " & objAdapter.DNSServerSearchOrder(i)
         arrDNSServers(i)=objAdapter.DNSServerSearchOrder(i)
      Next
      [green]'Here you must have aNewEntries well defined,
      'add your code to control if you retire aNewEntries[/green]
      redim preserve arrDNSServers(idim+ubound(aNewEntries)+1)
      For i = 0 to ubound(aNewEntries)
         arrDNSServers(idim+i+1)=aNewEntries(i)
      Next
      For Each objNetCard in colNetCards
         If not objNetCard.DHCPEnabled Then
            objNetCard.SetDNSServerSearchOrder(arrDNSServers)
         End If
      Next
      erase arrDNSServers
   End If
Next
regards - tsuji
 
Thank you for you help so far. I had to add a few lines to get the second netcard col to work. It works great, but I am just wondering one thing. Is there a way to make the new DNS entry the first entry in the search order and the stored entries to come after the new one?

Here is the vbscript so far with the extra lines:

dim arrDNSServers()
aNewEntries=array("***.***.***.***")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objAdapter in colAdapters
idim=0
If Not IsNull(objAdapter.DNSServerSearchOrder) Then
idim=ubound(objAdapter.DNSServerSearchOrder)
redim arrDNSServers(idim)
For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
' WScript.Echo " " & objAdapter.DNSServerSearchOrder(i)
arrDNSServers(i)=objAdapter.DNSServerSearchOrder(i)
Next
redim preserve arrDNSServers(idim+ubound(aNewEntries)+1)
For i = 0 to ubound(aNewEntries)
arrDNSServers(idim+i+1)=aNewEntries(i)
Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = TRUE")
For Each objNetCard in colNetCards
If not objNetCard.DHCPEnabled Then
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
End If
Next
erase arrDNSServers
End If
Next
 
blaster99,

I do not see why you have to renew a retrieval of the colNetCards. That is the only part I would comment on. As I do not see the reason for this, allow me to reason per my above script by incorporating this new search order.
Code:
dim arrDNSServers()
'Your new append list
aNewEntries=array("123.456.789.012","456.789.012.345")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
'n = 1    'not used, reactivate it if you need.
For Each objAdapter in colAdapters
   idim=0
   If Not IsNull(objAdapter.DNSServerSearchOrder) Then
      [green]'Here you must have aNewEntries well defined,
      'add your code to control if you retire aNewEntries[/green]
      idim=ubound(aNewEntries)
      redim arrDNSServers(idim)
      For i = 0 to idim
         arrDNSServers(i)=aNewEntries(i)
      Next
      redim preserve arrDNSServers(idim + ubound(objAdapter.DNSServerSearchOrder)+1)
      For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
         WScript.Echo "      " & objAdapter.DNSServerSearchOrder(i)
         arrDNSServers(idim+i+1)=objAdapter.DNSServerSearchOrder(i)
      Next
      For Each objNetCard in colNetCards
         If not objNetCard.DHCPEnabled Then
            objNetCard.SetDNSServerSearchOrder(arrDNSServers)
         End If
      Next
      erase arrDNSServers
   End If
Next
- tsuji
 
I only did this because without these extra statements it failed to run. It would stop at line 25 char 7 with error: Object not a collection code: 800A01C3 saying it was a vb runtime error. Once I added the extra lines of code this went away and the script worked correctly. Also I don't know if you say my other question. Is there a way to make the new entry the first in the search order?
 
blaster99,

OK, I take your words for it. I'll check it out.

Also, I have already changed the script per your requirement in search order. Modify it with your previous technique.

- tsuji
 
OK I have one last question, or at least I hope this is the last one. Is there a way to have this check for the new aNewEntries in the DNS search order, and if they are found to be present end the script? I would not want this to keep adding the same DNS entries again and again...
 
blaster99,

[1] I correct the mistake in my posting due to lazy cut and paste. I think that explain the reason why you have the runtime. Just keep the objNetCard the same all along within the single for loop.
[1a] Also you use different name objAdapter and objNetCard. I had just cut and paste. They are the same!
All these make a layback reading ridiculous. So I repost the second version of my posting.
[2] Also I make the figurative sample of ip more realistic.
[3] As nothing need be done for .DHCPEnabled true, should spare the work on the search data as well. Hence I place that check near the top.
Code:
dim arrDNSServers()
'Your new append list
aNewEntries=array("123.123.245.012","156.212.034.145")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
'n = 1    'not used, reactivate it if you need.
For Each objAdapter in colAdapters
   If not objAdapter.DHCPEnabled Then
      idim=0
      If Not IsNull(objAdapter.DNSServerSearchOrder) Then
         'Here you must have aNewEntries well defined,
         'add your code to control if you retire aNewEntries
         idim=ubound(aNewEntries)
         redim arrDNSServers(idim)
         For i = 0 to idim
            arrDNSServers(i)=aNewEntries(i)
         Next
         redim preserve arrDNSServers(idim + ubound(objAdapter.DNSServerSearchOrder)+1)
         For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
            WScript.Echo "      " & objAdapter.DNSServerSearchOrder(i)
            arrDNSServers(idim+i+1)=objAdapter.DNSServerSearchOrder(i)
         Next
      End If
      objAdapter.SetDNSServerSearchOrder(arrDNSServers)
      erase arrDNSServers
   End If
Next
- tsuji
 
blaster99,

To answer the uniqueness of ip in the dnsserversearchorder, you have just to check it before adding it. As uniqueness checking is very reasonable one, I take this opportunity to further correct mistakes of the last posting (really frustrating---all braincode and cut-and-paste in the textarea) and make further improvement in combining the conditionals. Here is the full revision.
Code:
'Version with uniqueness checked
dim arrDNSServers()
'Your new append list
aNewEntries=array("123.123.245.012","156.212.034.145")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
'n = 1    'not used, reactivate it if you need.
For Each objAdapter in colAdapters
   If (not objAdapter.DHCPEnabled) and (not IsNull(objAdapter.DNSServerSearchOrder)) Then
      idim=0
      'Here you must have aNewEntries well defined,
      'add your code to control if you retire aNewEntries
      idim=ubound(aNewEntries)
      redim arrDNSServers(idim)
      For i = 0 to idim
         arrDNSServers(i)=aNewEntries(i)
      Next
      For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
         WScript.Echo "      " & objAdapter.DNSServerSearchOrder(i)
         bDuplicate=false
         for j=0 to idim
            if arrDNSServerSearchOrder(i)=arrDNSServers(j) then
                bDuplicate=true : exit for
            end if
         next
         if not bDuplicate then
            redim preserve arrDNSServers(ubound(arrDNSServers)+1)
            arrDNSServers(ubound(arrDNSServers))=objAdapter.DNSServerSearchOrder(i)
         end if
      Next
      objAdapter.SetDNSServerSearchOrder(arrDNSServers)
      erase arrDNSServers
   End If
Next
- tsuji
 
Hello tsuji,

I got back to the office today and have been working with the script. I am having a problem. I am using the script that is supposed to check for dups. With the original code I get this error message.

Type mismatch: 'arrDNSServerSearchOrder'
Code: 800A000D

I have been and will keep playing with this to see if I can figure it out, but I am still just a newbie in the scripting world. Your help would be greatly appreciated.
 
blaster99,

You probably somewhere typed arrDNSServerSearchOrder (_wrong_) which should be arrDNSServers? Or you typed the method SetDNSServerSearchOrder as arrDNSServerSearchOrder (_wrong_)? Check typos.

- tsuji
 
I have checked for typos. I don't see any, I just copied and pasted your last revision of the script and then ran it. That is when I get the error. I have tried many different combinations and re-checked the spelling for typos and it still does not work yet. I am confident that between the two of us we'll figure it out. Though you will probably do it first.
 
blaster99,

OK, I will print code of my last posting out to read better.

- tsuji
 
blaster99,

I've revised the last script, corrected some mistakes, improved some functionality (allowing aNewEntries' entries being duplicated on itself---very unlikely) etc. Here is what I come up with from a proper text editor.
Code:
'Version with uniqueness checked
dim arrDNSServers()
'Your new append list
aNewEntries=array("123.123.245.012","156.212.034.145")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
'n = 1    'not used, reactivate it if you need.
For Each objAdapter in colAdapters
	if (not objAdapter.DHCPEnabled) then
		idim=0
		'Here you must have aNewEntries well defined,
		'add your code to control if you retire aNewEntries
		for i=0 to ubound(aNewEntries)
			bDuplicate=false
			if i=0 then
				redim arrDNSServers(0)
				arrDNSServers(i)=aNewEntries(i)
			else
				for j=0 to i
					if aNewEntries(i)=arrDNSServers(j) then
						bFound=true : exit for
					end if
				next
				if not bDuplicate then
					redim preserve arrDNSServers(ubound(arrDNSServers)+1)
					arrDNSServers(ubound(arrDNSServers))=aNewEntries(i)
				end if
			end if
		next
		if (not IsNull(objAdapter.DNSServerSearchOrder)) then
			for i = 0 To UBound(objAdapter.DNSServerSearchOrder)
				WScript.Echo "      " & objAdapter.DNSServerSearchOrder(i)
				bDuplicate=false
				for j=0 to ubound(arrDNSServers)
					If objAdapter.DNSServerSearchOrder(i)=arrDNSServers(j) then
						bDuplicate=true : exit for
					end if
				next
				if not bDuplicate then
					redim preserve arrDNSServers(ubound(arrDNSServers)+1)
					arrDNSServers(ubound(arrDNSServers))=objAdapter.DNSServerSearchOrder(i)
				end if
			next
		end if
		objAdapter.SetDNSServerSearchOrder(arrDNSServers)
		erase arrDNSServers
	end if
Next
- tsuji
 
Hey tsuji,

Well I still ran into a problem, though I got it figured out. I was getting a Subscript out of range: 'j' at line 18 char 20. I proceeded to rem out lines 18, 19 and 20. Once I remmed these lines out the script works beautifully. It does what I was looking for. Just to give you a quick reason on why I was trying to get the script like this. First they did not want to get rid of the old DNS entries, just incase they are there for a reason. Then being this script is going to be a login script I was concerned with getting multiple entries of the same dns entries. This is why I wanted to get the script to stop if it found a dup in there that matched the new dns entries. This allows this process to be a login script and run automatically without supervision. I am so thank full for you help and expertise. I am getting a little better with scripts day by day. I am sure I will be back to ask more questions. Hopefully though this will be the last for this particular script.
 
blaster99,

Correct the line
[tt] for j=0 to i[red]-1[/red][/tt]

I would prefer to have the checking it is supposed to be doing.

Yes, I see what you're doing and I appreciate your further explanation. Well explained. Thanks.

- tsuji
 
Awesome tsuji... With that edit it works as you intended it to. Thank you again for all of your help.

Sean
 
Hey tsuji,

I could use a little more of you help. The bosses have come back with yet another hurdle. I am going to be running this in a mixed environment of NT4, Win2k and WinXP. I need to have this script execute with admin rights. I do not know if this is possible or not but if it is can it be done without showing the password. I do not want to degrade the network security.

Sean
 
blaster99,

[0] One simple/simplistic solution would be to use alternative credential when establishing the objWMIService with admin rights. But, the password and account name are in the clear. Then you use the ms script encoder to make a .vbe file to let user running it. This is _very_ unsafe and fixed routine to decrypte it exists.

[1] The good solution would be to get free cpau of joeware:
Use it to create a job file (abc.job) with the command line:
[tt]wscript //nologo def.vbs[/tt]
with alternative higher authority user's account and passward. With the encrypted job file, your user runs it with the same cpau:
[tt]cpau -dec -file abc.job[/tt]
Hence, you pass on cpau and a job file to the location where your eventual user can have rights to read/execute. This is the preferred solution to get the job done.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top