Friday, January 23, 2009

Add Connection Specific dns suffix through command line

There is no direct command available in windows to add connection specific dns suffix for a particylar network connectin. However, one can achieve this by writing windows scripts.

The following snippet will add connection-specific dns suffix to a specific local are network connection. Copy the snippet and save it to a file say setdnssuffix.wsf and run as wscript setdnssuffix.wsf /DNSDomainSuffix:"mysuffix.com" on command prompt. here "mysuffix.com" is dns suffix need to be added.


You can download the script at SetDnsSuffix
< language="VBScript">
Public DNSDomainSuffix
Set colNamedArguments =WScript•Arguments.Named
DNSDomain = colNamedArguments.Item ("DNSDomainSuffix")
NetConnName = colNamedArguments.Item("netConnName")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapter")
For Each objItem in colItems
If Not IsNull(objItem.NetConnectionID) Then
if objItem.NetConnectionID = NetConnName Then
strMacAddress = objItem.MacAddress
end if
End If
Next
call ExecuteDnsSuffix (WMIEchoStr)
Function ExecuteDnsSuffix(WMIEchoStr)
strComputer = "•"
Set objWMIService = GetObject("winmgmts:"
& "{impersonationLevel=impersonate}!¯\\" & strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery –
("Select * From Win32–NetworkAdapterConfiguration Where IPEnabled =True")
For Each objNetCard in colNetCards
ExecuteDnsSuffix=objNetCard•SetDNSDomain(DNSDomain)
Next
WMIEchoStr=DNSDomain
End Function
<\script>
<\job>

2 comments:

  1. This is just what I need

    Do you have a way to tick the following boxes as well?

    - Register this connection's addresses in DNS

    - Use this connection's DNS suffix in DNS registration

    ReplyDelete
  2. netsh interface ipv4 set dnsservers name="Local Area Connection" source=dhcp register=both

    ReplyDelete