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.

[ps]
netsh interface ipv4 show dnsservers
[/ps]

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:

[ps]
netsh interface ipv4 delete dnsserver "Public" all
[/ps]

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

[ps]
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
[/ps]

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:

[ps]
netdom join %computername% /domain:mydomain /UserD:domainuser /PasswordD:secret
[/ps]

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.

[ps]
shutdown -r -f
[/ps]

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: