Monday, March 12, 2012

VBScript to enable "Use this connection's DNS Suffix in DNS registration" in IPV4

I have written this below VBScript to enable "Use this connection's DNS Suffix in DNS registration" in IPV4 Advanced Settings.

For Desktop adaptor, this (to enable "Use this connection's DNS Suffix in DNS registration" in IPV4) can be fixed by changing the registry value of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\NetworkInterfaceID\RegisterAdapterName to 1

Here NetowrkInterfaceID is a unique ID for every user and this can be obtained from "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\8\ServiceName" key.

The issue is there can be multiple Network adaptors connected to the machine like Network adaptor for Desktop, Wireless Adaptor, VMware adaptor etc and they all are stored under different hive, like 8, 12, 14 etc. Hence I have written a script which will check for all this and will change the value for all adaptors. If you want for only one particular adaptor, you can modify the script accordingly.

Following VB Functions can be independently picked too:
1) Read Registry
2) Registry Key exists
3) iteration in VBScript (For Loop)
4) Write Registry key.


On Error Resume Next

Dim NetworkInterfaceID, RegKeyValue, Temp

'HKEY_CURRENT_USER = HKCU
 'HKEY_LOCAL_MACHINE = HKLM
 'HKEY_CLASSES_ROOT = HKCR
 'HKEY_USERS = HKEY_USERS
 'HKEY_CURRENT_CONFIG = HKEY_CURRENT_CONFIG
Function KeyExists(key)
    Dim objShell
    On Error Resume Next
    Set objShell = CreateObject("WScript.Shell")
        objShell.RegRead (key)
    Set objShell = Nothing
    If Err = 0 Then KeyExists = True
End Function

For i = 0 to 20
  
RegistryKeyName = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" & i & "\"
If KeyExists(RegistryKeyName) Then
Reg= RegistryKeyName + "ServiceName"
NetworkInterfaceID = ReadReg(Reg)
RegKeyValue = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\" + NetworkInterfaceID + "\RegisterAdapterName"
WriteReg RegKeyValue, 1 ,"REG_DWORD"
End If
Next
 'WScript.Echo Temp
 Function WriteReg(RegPath, Value, RegType)
       'Regtype should be "REG_SZ" for string, "REG_DWORD" for a integer,…
       '"REG_BINARY" for a binary or boolean, and "REG_EXPAND_SZ" for an expandable string
       Dim objRegistry, Key
       Set objRegistry = CreateObject("Wscript.shell")

      Key = objRegistry.RegWrite(RegPath, Value, RegType)
       WriteReg = Key
 End Function
 Function ReadReg(RegPath)
       Dim objRegistry, Key
       Set objRegistry = CreateObject("Wscript.shell")

      Key = objRegistry.RegRead(RegPath)
       ReadReg = Key
 End Function

3 comments:

Morten said...

Hi.
Thanks for a great script.
How do I change the script so it only changes all apaters with a REG_SZ key called "Domain REG_SZ = vpn.domain.com"
Thanks
Morten

Morten said...

Hi.
Thanks for a great script.
How do I change the script so it only changes all apaters with a REG_SZ key called "Domain REG_SZ = vpn.domain.com"
This key is placed under:
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{INTERFACE......}]
Thanks
Morten

Piyush Nasa said...

You can search for this particular string in your script and if it matches then change it.