Hackviking He killed Chuck Norris, he ruled dancing so he took up a new hobby…

14Mar/170

Windows: Set DNS and add to domain from command line

Just got 18 virtual servers delivered from a private cloud supplier. Since none of them are joined to our domain I need to access them one by one and set them up. After they are joined to the domain it's easier to manage them. So I wanted a quick way to add our internal DNS servers and add them to the domain. Doing this manually is a time consuming task, error prone and straight up boring. So by doing this from the command line I could do it fast, correct and less boring.

There were three steps needed on each server:
1. Add all the DNS servers
2. Join the server to the domain
3. Reboot the server

First we need the name of the interface we are working with. It's easy enough but let's stay in the command line for this one.

netsh interface ipv4 show dnsservers

This will list all interfaces and it's DNS servers. Take note of the interface name, in my case Public, and the IP-addresses of the DNS servers you might need to remove before adding the correct ones. In my case both needed to go which gave me the option to have the first line of my "script" to be:

netsh interface ipv4 delete dnsserver "Public" all

This will remove all the DNS servers listed on the interface Public. Now I need to add the correct ones.

netsh interface ipv4 add dnsserver "Public" address=172.24.16.172
netsh interface ipv4 add dnsserver "Public" address=172.24.16.164
netsh interface ipv4 add dnsserver "Public" address=172.24.16.167
netsh interface ipv4 add dnsserver "Public" address=172.24.16.170
netsh interface ipv4 add dnsserver "Public" address=172.24.16.175
netsh interface ipv4 add dnsserver "Public" address=172.24.17.4
netsh interface ipv4 add dnsserver "Public" address=172.24.16.135
netsh interface ipv4 add dnsserver "Public" address=172.24.16.207
netsh interface ipv4 add dnsserver "Public" address=172.24.16.5

As you can see here you just create a line for each DNS you need to add. Then we need to join it to the domain:

netdom join %computername% /domain:mydomain /UserD:domain\user /PasswordD:secret

If you have special characters in your password, which you should have, you need to enclose the password in double quotes. Then we need to force a restart to get the domain change into effect.

shutdown -r -f

Just create all the steps in notepad, line by line,  and copy them to the servers cmd window and all but the last will execute. Just hit enter to execute the last one.

Posted by Kristofer Källsbo

Comments (0) Trackbacks (0)

No comments yet.


Leave a Reply

No trackbacks yet.