Introduction
This VB.NET console application allows you to:
- Set the DNS server configuration for a workstation or server, the server IPs are given as arguments to the application
- Set the MTU size for workstation or server
Background
In our environment we cannot use DHCP, Netsh will manage NIC configuration, but it is kludge and you need to specify the interface name. This tool finds the GUID for the DNS registered adapter(s) and updates the DNS servers. DnsConfig.exe may work with Vista, Windows 7 and Windows Server, but has not been tested on platforms other than XP. This tool is a good starting point for any command line tool that will take arguments and modify the Windows registry. I did not author the sub routines that modify the registry, write the log file and enumerate registry sub keys; they have been recycled.
Using the Code
Copy the tool to %systemroot%\system32:
- Argument usage:
- dnsConfig.exe <version> <MTU> <Primary DNS>,<Secondary DNS> <log file path>
- Examples:
- DNS only
- dnsConfig.exe 2 192.168.0.1,192.168.0.2 \\192.168.15.25\logs
- MTU only
- dnsConfig.exe 2 MTU:1500 \\192.168.15.25\logs
- MTU + DNS
- dnsConfig.exe 2 MTU:1500 192.168.0.1,192.168.0.2 \\192.168.15.25\logs
DNS server entries are not limited. The DNS server argument becomes the NameServer registry value.
Public Function Argument(ByVal args() As String)
If args Is Nothing OrElse args.Length < 4 OrElse _
args.Length > 5 Then
Call ShowHelp()
Return False
Else
If args.Length = 5 Then
dnsVer = args(1)
MTU = args(2).Substring(4)
DNS_Values = args(3)
Log_Path = args(4)
oprDNS = True
oprMTU = True
End If
If args.Length = 4 Then
If args(2).CompareTo("MTU:") = 1 Then
dnsVer = args(1)
MTU = args(2).Substring(4)
Log_Path = args(3)
oprMTU = True
Else
dnsVer = args(1)
DNS_Values = args(2)
Log_Path = args(3)
oprDNS = True
End If
End If
Return True
End If
End Function
Argument Function
The function above handles the arguments. This function could be leveraged for any console application with a different quantity or type of input.
Public Function GetAllChildSubKeys(ByVal MainKey As RegistryKey, _
ByVal sKey As String) As ArrayList
Dim rkKey As RegistryKey
Dim sSubKeys() As String
GetAllChildSubKeys = Nothing
rkKey = MainKey.OpenSubKey(sKey)
sSubKeys = rkKey.GetSubKeyNames
For Each s As String In sSubKeys
If oprDNS = True Then
Call MyRegistry(Registry.LocalMachine, _
"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" _
+ (s), "NameServer", DNS_Values, "W", RegistryValueKind.String)
End If
If oprMTU = True Then
Call MyRegistry(Registry.LocalMachine, _
"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" _
+ (s), "MTU", MTU, "W", RegistryValueKind.DWord)
End If
Next
If sSubKeys.Length() > 1 Then
Dim strComputerName As String
strComputerName = Environment.GetEnvironmentVariable("ComputerName")
Call WriteData("ERROR -- Multiple DNS registered adapters", _
Log_Path, Log_Path + "\" + strComputerName + ".txt", True)
End If
End Function
Sub Key Function
The function above builds a collection of sub keys and updates the DNS and MTU values depending on the arguments provided:
Public Function MyRegistry(ByVal MainKey As RegistryKey, ByVal sKey As String, _
ByVal Value As String, ByVal Data As String, ByVal RorW As String, _
ByVal RegistryValueKind As RegistryValueKind)
Dim regKey As RegistryKey
MyRegistry = Nothing
regKey = MainKey.CreateSubKey(sKey)
If RorW = "W" Then
regKey.SetValue(Value, Data, RegistryValueKind)
regKey.Close()
End If
If RorW = "R" Then
MyRegistry = regKey.GetValue(Value)
regKey.Close()
End If
End Function
The function above is used to read or write to/from the Windows registry.
It allows control of the value type created:
- String Value
- Binary Value
- Dword Value
- Multi-String Value
- Expandable String Value
Example
MyRegistry(<Hive>,<Registry Key>,<Value Name>,<Data>,<Operation>,<Value Type>)
MyRegistry(Registry.LocalMachine, _
"SYSTEM\ControlSet001\Services\Tcpip", "dnsVer", "3.5", "W", RegistryValueKind.String)
Points of Interest
.NET has security restrictions that prevent console applications from being executed across the network; the application needs to reside on the local system. My original intention was to call the application with group policy and pass the arguments from group policy. Due to the security restrictions I ended up with a Vbscript in group policy that copies the application to the workstation and then calls it with arguments. Of course, this could all have been done in Vbscript, PowerShell, etc., but I want to learn VB.NET. Visual Developer 2008 is so efficient it has removed the time barrier for compiling code, with compiled admin tools you don't have to worry about another admin altering the code or administering signed scripts, etc.
History
- 11th June, 2009: Initial post on CodeProject
- 12th June, 2009: Updated article