I have been automating a bit more recently regarding desktop deployments. DNS and DNS Suffix were two items that needed adjustment
Thankfully setting the DNS for an adapter was quite simple and I could just utilize a batch file like so:
netsh interface ipv4 add dnsservers "Local Area Connection" 192.55.55.1 netsh interface ipv4 add dnsservers "Local Area Connection" 192.55.55.2
Well lets just figure out the netsh command for DNS Suffix. Oh wait, there isn’t one! Well, after some investigation the registry for me appeared like the next best way to tackle the Suffix. The settings for the interfaces are set up here:
SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces
I believe there is a Searchlist string right within the parent folder (Parameters) that may set the suffix globally. I don’t know if there is any way of setting this value through the GUI (perhaps this is a Group Policy thing) and I did not want to set the DNS for anything other than the LAN connection so I had to look elsewhere.
Within the Interfaces key, there are GUID’s (A random unique ID) that denote an adapter. Take a look for yourself. The way I did it was that I changed the DNS via the netsh command, then did a loop through the interface keys in C# to find the right one and change the suffix there. This could be purely done with a batch file but for me it was quicker to do it this way.
const string DNS_SUFFIX = "mydomain.ca"; string[] interfaceKeys; Microsoft.Win32.RegistryKey key, key2; key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces", true); Console.Out.WriteLine("Changes dns suffix for any connection with 192.55.55.1 DNS to mydomain.ca"); Console.Out.WriteLine("Interfaces found: " + key.SubKeyCount); interfaceKeys = key.GetSubKeyNames(); for (int i = 0; i < key.SubKeyCount; i++) { try { key2 = key.OpenSubKey(interfaceKeys[i], true); if (key2.GetValue("NameServer", "").ToString().Contains("192.55.55.1")) { Console.Out.WriteLine("Setting MYDOMAIN.CA dns suffix for interface which has 192.55.55.1 DNS detected: " + interfaceKeys[i].ToString()); key2.SetValue("Domain", "mydomain.ca"); Console.Out.WriteLine("Success"); } key2.Close(); } catch (Exception ex) { Console.Out.WriteLine("Error: " + ex.Message); } } key.Close(); System.Threading.Thread.Sleep(4000);
Note: This program must be run as administrator in order to successfully modify the registry
No Comments
There are no comments related to this article.
Leave a Reply