Search This Blog

Tuesday, May 31, 2011

Windows: Add multiple DNS servers to NIC (netSh)

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…. Smile

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


Share/Bookmark

8 comments:

  1. hi.. i'm working at campus, we give wifi free to student and sudenly virus spead and became rouge dhcp pointing dns to its own (after changing dns setting).

    and idea to make a script to change all adapter dns setting in winxp and win7 to our official dns?
    is it= "%ALL%" or "%*%" ?
    thanks.. if u mind, pelase send to my mail: sabriptm@yahoo.com

    ReplyDelete
  2. I have been away so have only just picked this up.

    The Netsh command relies on knowing the adapter name, I don't believe you can just do a wildcard and apply to all.

    Quickly thinking, you may be able to list out all the adapters and dump into a list which you can use, but this will take some scripting to achieve. This list could then be used for
    SET AdapterNames=.....

    If you have managed to sort this out already then I would be interested in your solution.

    ReplyDelete
  3. I found the best way to accomplish this without knowing the adapter name upfront is to use IPCONFIG and FIND. Example: IPCONFIG | FIND /I "Ethernet adapter".

    An example output would be:
    Ethernet adapter Local Area Connection:
    Ethernet adapter Local Are Connection #2:
    Ethernet adapter ManuallyRenamedLANC:

    Then use a FOR loop while using ":" as the delimiter. Since "Ethernet adapter" is a fixed size for all outputs you just have to remove those characters from each string. I will reply with a finished script when I do it. This task was just assigned to me.

    A side note, if you are using Windows 7/Vista/Server 2008 then you can use WMIC /NODE:%REMOTECOMPUTER% process call "%call%" instead of PSEXEC. WMIC is native and requires no Sysinternals download.

    ReplyDelete
  4. Here it is:

    :: Sets the primary and alternate DNS for IPv4 on Windows Server 2000/2003/2008 & Windows XP/Vista/7
    @ECHO OFF
    SETLOCAL EnableDelayedExpansion

    SET adapterName=

    FOR /F "tokens=* delims=:" %%a IN ('IPCONFIG ^| FIND /I "ETHERNET ADAPTER"') DO (
    SET adapterName=%%a

    REM Removes "Ethernet adapter" from the front of the adapter name
    SET adapterName=!adapterName:~17!

    REM Removes the colon from the end of the adapter name
    SET adapterName=!adapterName:~0,-1!

    netsh interface ip set dns name="!adapterName!" static x.x.x.x primary
    netsh interface ip add dns name="!adapterName!" x.x.x.x index=2
    )

    ipconfig /flushdns

    :EOF

    ReplyDelete
  5. Nice Skuzzy, thanks for the comment. I like that a lot... :o)

    Thanks for the heads up on the wmic, had not seen that before... just always used psexec... will look into that some more.

    ReplyDelete
  6. Hi
    Neither of these work for me for some reason?


    C:\pstools\Change DNS>(
    IF !Index! EQU 1 (Netsh interface ip set dns name="!vAdaptername!" source="static" addr="10.133.34.30" )
    IF !Index! GTR 1 (Netsh interface ip add dns name="!vAdaptername!" addr="10.133.34.30" index=!Index! )
    SET /A Index=!Index!+1
    )

    Invalid interface Local Area Connection specified.


    C:\pstools\Change DNS>(
    IF !Index! EQU 1 (Netsh interface ip set dns name="!vAdaptername!" source="static" addr="10.133.48.30" )
    IF !Index! GTR 1 (Netsh interface ip add dns name="!vAdaptername!" addr="10.133.48.30" index=!Index! )
    SET /A Index=!Index!+1
    )

    Invalid interface Local Area Connection specified.


    On the second example set by Skuzzy, I get the following error

    C:\pstools\Change DNS>autosetdns2.bat
    | was unexpected at this time.
    C:\pstools\Change DNS>

    Can someone explain why?
    thanks

    ReplyDelete
  7. 'Local Area Connection' is the name of all my nics across all my servers. Ive tried running this on my xp machine, win7 and 2008 r2. Servers are Win2k3 or Win2k8r2

    ReplyDelete
  8. Steve, if you're still having this problem with my example make sure you have an up carrot "^" before the pipe "|". Batch will interpret the pipe as a pipe instead of a character if not preceded with the "^".

    ReplyDelete