Note: I have now changed the script slightly. I have added a IPCONFIG /ALL at the top, this allows me to easily review the connection name (and also copy and paste into the input field if needed).
Also I have found the address=”” parameter is not valid in versions prior to netsh in windows 2008, so now it is just addr=””
I have highlighted the changes in PURPLE in the script below.
Recently I had to configure a number of servers. I had to change the dns server IP addresses.
Now I did not want to go through all the servers 1 by 1, changing the IPs in the GUI.
Enter left, Netsh. This great little tool, allows you to configure a whole range of things within windows. But in my case I was just interested in dns servers.
First command sets the default dns server ip, then the following commands add the additional dns servers and specifies where they sit in the order via index=n.
Netsh interface ip set dns name="<connectionname>" source="static" address="x.x.x.x"
Netsh interface ip add dns name="Team 1" addr="x.x.x.x" index=2
Netsh interface ip add dns name="Team 1" addr="x.x.x.x" index=3
Netsh interface ip add dns name="Team 1" addr="x.x.x.x" index=4
Now put this into a batch file and we are laughing. Login and run, job done.
Now this does assume all servers will have a network adapter named the same thing, so I may have to change if I come across a different adapter name. mmmmm, that gets me thinking I can get round that by setting up an input into the batch file……
OK, so I did do that and here is the batch file contents. It prompts for a interface name (it has a default setting, as most of the NICs in my setup have the same name), also takes an comma separated list of dns server ip’s. The first in the server ip list will be the default dns server, and then the rest will be added in order (so the last in the list in the batch file will be the last in the dns server list).
So if you are going to use this make sure you change the list to dns server ips and also change the defaultNIC variable to your most common NIC name.
IPCONFIG /ALL
SETLOCAL ENABLEDELAYEDEXPANSION
SET DNSServerIPaddresses=10.0.0.1,10.0.0.2,10.0.0.3,10.0.0.4
SET DefaultNIC=Nic1
SET /A Index=1
:InputNetworkAdapter
SET /p vAdapterName=Please enter network adapter name (default="%DefaultNIC%") :-
FOR %%A IN (%DNSServerIPaddresses%) DO (
IF !Index! equ 1 (
Netsh interface ip set dns name="%vAdapterName%" source="static" addr="%%A"
)
IF !Index! gtr 1 (
Netsh interface ip add dns name="%vAdapterName%" addr="%%A" index=!Index!
)
SET /A Index=!Index!+1
)
mmm, this has got me thinking now…. with some psexec magic should be able to get this done remotely without having to connect to each machine….. O well I will save that for another day, I need to get this moving….
addition – run remotely on multiple servers.
OK, so I did use psexec and it worked a treat. I create two batch files and copied psexec into the same folder. Then I create a list of servers names, the script will prompt for a serverlist. I also created a subfolder called reports, I crate textfile of the stdout of the psexec command. the output contains a ipconfig /all berfore and after running the netsh lines, this will allow for reviewing and you can ensure that the change has held.
This script should happily work on windows 2000, 2003 and 2008 servers.
startAutoDNS.bat
:InputServerList
SET /p vserverList=Please enter filename of server list (default="serverlist.txt") :-
IF "%vserverList%"=="" (
SET vserverList=serverlist.txt
)
FOR /F "eol=# tokens=1 delims=," %%A IN (.\%vserverList%) DO START CMD /C ".\psexec \\%%A -s -f -c autosetDNS.bat>reports\output_setDNS_%%A.txt"
autoSetDNS.bat
SETLOCAL ENABLEDELAYEDEXPANSION
IPCONFIG /ALL
SET AdapterNames=Team 1,Local Area Connection,Local Area Connection 2,Local Area Connection 3,Local Area Connection 4
REM substitute
SET AdapterNames=%AdapterNames: =/%
SET DNSServerIPaddresses=10.0.0.1,10.0.0.2,10.0.0.3,10.0.04
FOR %%B IN (%AdapterNames%) DO (
SET /A Index=1
SET vAdaptername=%%B
SET vAdaptername=!vAdaptername:/= !
FOR %%A IN (%DNSServerIPaddresses%) DO (
IF !Index! equ 1 (
Netsh interface ip set dns name="!vAdaptername!" source="static" addr="%%A"
)
IF !Index! gtr 1 (
Netsh interface ip add dns name="!vAdaptername!" addr="%%A" index=!Index!
)
SET /A Index=!Index!+1
)
)
IPCONFIG /ALL