?
Solved

script for changing DNS

Posted on 2003-03-03
40
Medium Priority
?
394 Views
Last Modified: 2010-08-05
I would like to change the DNS setting for all computers (XP and 98) in a Non-DHCP environment.  Due to the computers are distributed in different locations,thus, i am trying to find an easy way to change the user's DNS setting. I would think running a script would be one way. Since i am not a expert in programing,so, I have hard time to write a simple script to edit the registry. If anyone who have experience or suggestion on how to achieve this, GREATLY appreciated!!!

Thanks in advance!!!!

Eva
0
Comment
Question by:meesick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 25
  • 14
40 Comments
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8060201
Hi. You can use the following script to assign DNS servers to all NICs on the local machine. Just copy it to a file and save it with .vbs extention. Don't forget to reboot after executing.


Const strNewDNS1 = "127.0.0.1"
Const strNewDNS2 = "127.0.0.2"

Set objNICs = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_NetworkAdapterConfiguration")

For Each objNIC In objNICs
     If objNIC.IPEnabled Then
          objNIC.SetDNSServerSearchOrder Array(STR_NEWDNS1,STR_NEWDNS2)
     End If
Next

Regards,
Zhenya
0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8060209
Sorry. Line 8:
objNIC.SetDNSServerSearchOrder Array(STR_NEWDNS1,STR_NEWDNS2)
should look like
objNIC.SetDNSServerSearchOrder Array(strNewDNS1,strNewDNS2)
0
 

Author Comment

by:meesick
ID: 8065212
You are the best! But, i still some problem with XP. How can i run the script in XP.  I tried to double click it or use DOS command to get it run but failed.  The XP machines just opened the file instead run the script.  What I do with it?  

Again, thank you so much!!!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
ZhenyaS earned 800 total points
ID: 8065562
Hmmmm...
1. Maybe Windows Scripting Host was not installed or was deleted.
2. Maybe the extention association was re-assigned. Do you see that .vbs extention is associated with WScript.exe?
Anyway you have to execute that script with WScript.exe so you can run: "wscript.exe DNSChangeScript.vbs"
3. Also you can try to change DNS servers remotely. Change line 3:
Set objNICs = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_NetworkAdapterConfiguration")
to:
Set objNICs = GetObject("winmgmts:{impersonationLevel=impersonate}!//<MachineName>").InstancesOf("Win32_NetworkAdapterConfiguration")

<MachineName> should be changed of course. So if machine name is CLIENT001 the code line should be:
Set objNICs = GetObject("winmgmts:{impersonationLevel=impersonate}!//client001").InstancesOf("Win32_NetworkAdapterConfiguration")

Good luck.
Zhenya
0
 

Author Comment

by:meesick
ID: 8065653
Thank you sooo much!!!!! ^o^  Got it working!!!!!
0
 

Author Comment

by:meesick
ID: 8067102
Hi Zhenya:
I am sorry, could you help me a bit further??  Could this vbs program convert to a DOS execute program in order to run it in XP which doesn't have VB enable?  It is because the program doesn't run vbs extension,except manually enable VB in the computer's setting.

Again,thanks very very very much for your help!
0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8067427
Did you try to run "wscript.exe <ScriptName>.vbs"?
Did you try to change DNS remotely?

If it didn't work can you please tell me how do you enable scripting?
0
 

Author Comment

by:meesick
ID: 8067456
Hi Zhenya:
I am sorry, could you help me a bit further??  Could this vbs program convert to a DOS execute program in order to run it in XP which doesn't have VB enable?  It is because the program doesn't run vbs extension,except manually enable VB in the computer's setting.

Again,thanks very very very much for your help!
0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8069616
Hi. You didn't answer my questions...
But anyway if you can't use scripting then you can try to use NetShell administration tool.
Run:
netsh interface ip set dns "Local Area Connection" static <DNSServer1>
It will erase all current DNS entries and add <DNSServer1>.
There is also command:
netsh interface ip add dns name = "Local Area Connection" <DNSServerN>
to add additional servers but on my test XP Prof. machine unfortunately it didn't work. It added <DNSServerN> to WINS servers :-(
0
 

Author Comment

by:meesick
ID: 8088426
Sorry, Zhenya. In the past two days,I am the one in my IT Department and got lots of different issues going on in the Network, thus, didn't have time to catch up what you suggest me to do!  But, i will try what you have posted NOW! and let you know!
Thanks!
0
 

Author Comment

by:meesick
ID: 8088580
I have posted the same question under another title, and have shared your solution and have given credit you too. I just want to let you know too, here is the link:

http://www.experts-exchange.com/Networking/WinNT_Networking/Q_20536913.html

May be you are interested to take a look too!

Thanks, Zhenya~~

Eva
0
 

Author Comment

by:meesick
ID: 8088739
I had tried out to re-assign the extension association, then, i worked out in other XP mahcines. But, i can't physically access all the computers to change the extension for all the computers.  Is there a way to do change the extension before the vbs script execute?

regarding the remote DNS, my environment is using static IP but not DHCP, then if i put my mahine name into your Remote DNS script,  how does it work out?

Thanks!
0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8088758
Hi, Eva.
Thanks for sharing my solution. Hope it will help somebody.
Zhenya

P.S. You misspelled the word "Exchange" in the other post ;-)
0
 

Author Comment

by:meesick
ID: 8088813
want to add a note too:

the Remote DNS script just like the normal one.  

I am looking at the NetShell administration tool, as you suggest and let you know how it works out!

keep you posted! THX
0
 

Author Comment

by:meesick
ID: 8088835
Make the correction to that link. =P
0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8088841
Regarding file extention association:
I think it would be easier for you just to run
"wscript.exe <ScriptName>.vbs"

Regarding remote DNS change:
change line:
Set objNICs = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_NetworkAdapterConfiguration")
to
Set objNICs = GetObject("winmgmts:{impersonationLevel=impersonate}!//<MachineName or IP>").InstancesOf("Win32_NetworkAdapterConfiguration")
where <MachineName or IP> is MachineName or machine IP address where DNS settings should be changed.
0
 

Author Comment

by:meesick
ID: 8088847
Make the correction to that link. =P
0
 

Author Comment

by:meesick
ID: 8088848
let me try it now!
0
 

Author Comment

by:meesick
ID: 8088932
hum...., i got error saying "Permission Deny getObject"
0
 

Author Comment

by:meesick
ID: 8089033
I had looked at the NetShell. Thank you soooo much! That's really helpful ~~~
By the way, the vbs's problem had been solved~ Thank you very much!!! got it working smoothly~~~

Got another way around.  I converted the .vbs file to .exe file. Then, distributing the .exe file to XP machines.  

0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8089053
Regarding "Permission Deny" problem:
I guess the user account you are trying to run the script under should have administrative priviledges on the machine where you want DNS settings to be changed.
0
 

Author Comment

by:meesick
ID: 8089124
in the NetShell, I tried to typ in this:
netsh interface ip set dns "Local Area Connection" static <DNSServer1> primary

Do you know how can i put in the secondary DNS? I had tried serverl combinations like this:

netsh interface ip set dns "Local Area Connection" static <DNSServer1> seconary

but, doesn't work....

0
 

Author Comment

by:meesick
ID: 8089145
I also tried:


netsh interface ip set dns "Local Area Connection" static <DNSServer1> < DNSServer1> both

doesn't work also...hum...
0
 

Author Comment

by:meesick
ID: 8089155
oops...

netsh interface ip set dns "Local Area Connection" static <DNSServer1> < DNSServer2> both
0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8089157
Read my post about NetShell.
Hear is quote:
"...
There is also command:
netsh interface ip add dns name = "Local Area Connection" <DNSServerN>
to add additional servers but on my test XP Prof. machine unfortunately it didn't work. It added <DNSServerN> to WINS servers :-( "
0
 

Author Comment

by:meesick
ID: 8089648
oops...

netsh interface ip set dns "Local Area Connection" static <DNSServer1> < DNSServer2> both
0
 

Author Comment

by:meesick
ID: 8089671
May be i misunderstand you..

the command "netsh interface ip set dns "Local Area Connection" static <DNSServer1>" did work in my XP... it changed the DNS config.
0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8089705
Those are two different commands:
netsh interface ip set dns "Local Area Connection" static <DNSServer1>
and
netsh interface ip add dns "Local Area Connection" <DNSServerN>

The first one with SET word (between "ip" and "dns") erases all current DNS servers and adds DNSServer1.

Second one with ADD word (between "ip" and "dns") adds additional DNS servers but it didn't work on my test machine.
0
 

Author Comment

by:meesick
ID: 8089906
Ok. if i want to the first comment to reset two DNS address.  How could i do it?
netsh interface ip set dns "Local Area Connection" static <DNSServer1> <DNSServer2> both

is that right? does it work in your xp?

thanks, ZhenyaS
0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8090104
NetShell tool can set/add DNS servers one by one only.
So to erase current DNS configuration and add first new DNS server you should use:
netsh interface ip set dns "Local Area Connection" static <DNSServer1>
That command worked for me.

To add additional DNS servers and don't change existing configuration you should use:
netsh interface ip add dns "Local Area Connection" <DNSServerN>

So in the case when you want to have 2 DNS servers and erase current DNS configuration you should run:
netsh interface ip set dns "Local Area Connection" static <DNSServer1>
and then
netsh interface ip add dns "Local Area Connection" <DNSServerN>

But the second command didn't work properly on my test machine.

Zhenya
0
 

Author Comment

by:meesick
ID: 8090240
ic!! greatly appreciated your help and explaination!  That helps alot!!!
0
 

Author Comment

by:meesick
ID: 8090290
i tried the" netsh interface ip add dns "Local Area Connection" <DNSServerN>", it doens't work on my XP too!

again, thanks!

0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8090427
Hey. I found a work around that problem.
We have 2 WINS servers on the network. Every time I was executing "dns add" command my second WINS server got overwritten. So just to give it a shot I deleted second WINS server and ran "dns add" command and it worked!!!

So the guide for you to add 2nd DNS server:
1. netsh interface ip delete wins "Local Area Connection" <WINSServerN>
2. Repeat step 1 for all WINS servers except 1
3. netsh interface ip add dns "Local Area Connection" <DNSServerN>
4. netsh interface ip add wins "Local Area Connection" <WINSServerN>
5. Repeat step 4 for all WINS servers deleted in step 1 and 2.

You can put all those commands to .bat file.

Good luck,
Zhenya
0
 

Author Comment

by:meesick
ID: 8090748
We don't have WINS setup.  
0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8090932
Hmmmm... If I delete all WINS servers from TCP/IP configuration then I don't have any problems with adding additional DNS servers useing "dns add" command. Do you get any error message? or just "Ok."?
0
 

Author Comment

by:meesick
ID: 8097782
Sorry for the late reply.

I was checking the other forums where you have joined into the dicussion.  (I should give you an A++++ expert if you could understand what i mean.)
 
l can tell you tomorrow what error message i got when i use "dns add" command.  But, i am sure i didn't get an OK.


Enjoy your SAT and Sunday.  Have a nice day!

THX ^^
0
 

Author Comment

by:meesick
ID: 8102909
I have tried it out, it works out fine if i put index=2 after the add dns command.  Interesting!! thanks, ZhenyaS!!  Do you have any MSN, ICQ or yahoo accounts? I would like to keep in touch with you if you don't mind.
0
 
LVL 1

Expert Comment

by:ZhenyaS
ID: 8105128
Glad you got it working.
I use MSN. Give me your e-mail.
0
 

Author Comment

by:meesick
ID: 8105552
sun_me824@hotmail.com
0
 

Expert Comment

by:mmixon
ID: 14444050
I realize this issue has probably been closed but I wanted to know if you have a vbscript to enable dhcp for the dns servers?  I have a script that enables dhcp for the ipaddr and gway, but static ns entries remain. would i just leave the newdns1 and 2 entries blank in that case or is the syntax different?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Introduction to Processes

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question