VBScript: Append New DNS Suffixes to the DNS Suffix Search Order
I am new to scripting and trying to do the following in VBScript: I have to read the DNS Suffixes that are already on the system, and then add two new ones to the end of the DNS Suffix Search Order list. I also have to make sure not to add the two new DNS suffixes if they are already on the DNS Suffix List on the system.
I have looked at the following site to "come up with" the following scripts (this site may also help you): http://www.microsoft.com/technet/scriptcenter/topics/networking/05_atnc_dns.mspx#EMGAE
Note: this script will replace the current DNS Suffixes with the ones I supply. I actually need the DNS Suffixes I supply to be appended to the ones already on the system. I would really appreciate any help anyone can give me.
On Error Resume Next
strComputer = "."
arrNewDNSSuffixSearchOrder = Array("abc.de.com", "fgh.ij.com")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
Set objNetworkSettings = _
objWMIService.Get("Win32_NetworkAdapterConfiguration")
intSetSuffixes = _
objNetworkSettings.SetDNSSuffixSearchOrder(arrNewDNSSuffixSearchOrder)
This script will display the current DNS Suffix List on the system:
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objNicConfig In colNicConfigs
If Not IsNull(objNicConfig.DNSDomainSuffixSearchOrder) Then
For Each strDNSSuffix In objNicConfig.DNSDomainSuffixSearchOrder
WScript.Echo strDNSSuffix
Next
End If
Next
I have looked at the following site to "come up with" the following scripts (this site may also help you): http://www.microsoft.com/technet/scriptcenter/topics/networking/05_atnc_dns.mspx#EMGAE
Note: this script will replace the current DNS Suffixes with the ones I supply. I actually need the DNS Suffixes I supply to be appended to the ones already on the system. I would really appreciate any help anyone can give me.
On Error Resume Next
strComputer = "."
arrNewDNSSuffixSearchOrder = Array("abc.de.com", "fgh.ij.com")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
Set objNetworkSettings = _
objWMIService.Get("Win32_NetworkAdapterConfiguration")
intSetSuffixes = _
objNetworkSettings.SetDNSSuffixSearchOrder(arrNewDNSSuffixSearchOrder)
This script will display the current DNS Suffix List on the system:
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objNicConfig In colNicConfigs
If Not IsNull(objNicConfig.DNSDomainSuffixSearchOrder) Then
For Each strDNSSuffix In objNicConfig.DNSDomainSuffixSearchOrder
WScript.Echo strDNSSuffix
Next
End If
Next
0 Comments
[ + ] Show comments
Answers (10)
Please log in to answer
Posted by:
anonymous_9363
15 years ago
I haven't tested this at all, it's just a flow-of-consciousness kind of thing. The essence is to create a string containing all the suffixes and convert THAT string to an array, rather than creating an array of the new address and trying to shoe-horn the existing ones into that. That gets you into all sorts of mess, like redimensioning arrays, or copying the contents of one array to another.
Anyway, if it fails, I'm sure you can work out the gist. Lastly, remember to add some error-trapping for your object creation, for string length and so on.
Anyway, if it fails, I'm sure you can work out the gist. Lastly, remember to add some error-trapping for your object creation, for string length and so on.
Option Explicit
Dim strComputer
Dim strNewDNSSuffixSearchOrder
Dim arrNewDNSSuffixSearchOrder
Dim objWMIService
Dim colNicConfigs
Dim objNetworkSettings
Dim intSetSuffixes
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
Set objNetworkSettings = objWMIService.Get("Win32_NetworkAdapterConfiguration")
For Each objNicConfig In colNicConfigs
For Each strDNSSuffix In objNicConfig.DNSDomainSuffixSearchOrder
'// Add the existing suffixes to the new ones.
'// You may want to reverse that order.
If Len(strNewDNSSuffixSearchOrder) = 0 Then
strNewDNSSuffixSearchOrder = strDNSSuffix
Else
strNewDNSSuffixSearchOrder = strNewDNSSuffixSearchOrder & "," & strDNSSuffix
End If
Next
Next
'// Create an array from the newly-elongated string
arrNewDNSSuffixSearchOrder = Split(strNewDNSSuffixSearchOrder, ",")
'// Pass the array to the 'SetDNSSuffixSearchOrder' method
intSetSuffixes = objNetworkSettings.SetDNSSuffixSearchOrder(arrNewDNSSuffixSearchOrder)
On Error Goto 0
Set objNetworkSettings = Nothing
Set colNicConfigs = Nothing
Set objWMIService = Nothing
Posted by:
bmonroe924
15 years ago
Here is one i have used recently that works pretty good.
Dim sKey
Dim sValue
Dim sNewData
Dim sbackup
skey = "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\"
sValue = "SearchList"
sNewData = "zone.test.com"
sbcakup = "Backup"
with CreateObject("WScript.Shell")
on error resume next
sdata = .RegRead(skey & sValue)
on error goto 0
if Instr(sData, snewdata) <> 0 then
' wsh.echo sNewdata, "is already present"
else
if sData <> "" then sNewdata = "," & sNewdata
.RegWrite skey & sbackup, sData & sNewdata, "REG_SZ"
.RegWrite skey & sValue, sData & sNewdata, "REG_SZ"
' wsh.echo ".RegWrite " & skey & sValue, sData & sNewdata, "REG_SZ"
end if
end with ' WSHShell
Dim sKey
Dim sValue
Dim sNewData
Dim sbackup
skey = "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\"
sValue = "SearchList"
sNewData = "zone.test.com"
sbcakup = "Backup"
with CreateObject("WScript.Shell")
on error resume next
sdata = .RegRead(skey & sValue)
on error goto 0
if Instr(sData, snewdata) <> 0 then
' wsh.echo sNewdata, "is already present"
else
if sData <> "" then sNewdata = "," & sNewdata
.RegWrite skey & sbackup, sData & sNewdata, "REG_SZ"
.RegWrite skey & sValue, sData & sNewdata, "REG_SZ"
' wsh.echo ".RegWrite " & skey & sValue, sData & sNewdata, "REG_SZ"
end if
end with ' WSHShell
Posted by:
Jsaylor
15 years ago
Posted by:
anonymous_9363
15 years ago
According to MSDN, it's Win2K and above. From my reading, EnableDNS is used for just that purpose - enabling DNS. The ability to add suffixes is tagged on, as it were. Think of it as a one-stop shop, mimicing the user sitting in front of the UI, clicking 'Obtain an IP address automatically' and completing the dialog. SetDNSSuffixSearchOrder is the means to just add suffixes, where DNS is already enabled.
Carry on as you are.
Carry on as you are.
Posted by:
Jsaylor
15 years ago
Posted by:
Nomi1985
15 years ago
Thanks for your help guys. I really appreciate it.
@ VBScab: I haven't been able to figure out why the code you posted would not work, but I'm sure I just need to spend a little more time on it and I should be able to figure it out.
@ bmonroe924: The code worked great, and I really liked how intuitive it was to read/understand it. There were two variable name errors, but I fixed that. In your example you only add one DNS Suffix, but I needed to add two, so I just repeat the code.
Dim sKey
Dim sValue
Dim sData
Dim sBackup
Dim sCreateBackup
Dim sNewData1
Dim sNewData2
sKey = "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\"
sValue = "SearchList"
sNewData1 = "abc.de.com"
sNewData2 = "fgh.ij.com"
sBackup = "Backup"
sCreateBackup = TRUE
with CreateObject("WScript.Shell")
on error resume next
'// Read the current DNS Suffix List
sData = .RegRead(sKey & sValue)
on error goto 0
'// Check to see if DNS Suffix is already on the system
if Instr(sData, sNewData1) <> 0 then
' wsh.echo sNewdata1, "is already present"
else
if sData <> "" then sNewdata1 = "," & sNewdata1
'// Backup the current DNS Suffix List before any changes are made.
'// Set sCreateBackup to FLASE so that a backup is not made each time a DNS Suffix is added
if sCreateBackup = TRUE then
.RegWrite sKey & sBackup, sData, "REG_SZ"
sCreateBackup = FALSE
end if
'// Write the DNS Suffix you want to add to the system
.RegWrite sKey & sValue, sData & sNewdata1, "REG_SZ"
' wsh.echo ".RegWrite " & skey & sValue, sData & sNewdata1, "REG_SZ"
end if
'// Read the current DNS Suffix List
sData = .RegRead(sKey & sValue)
on error goto 0
'// Check to see if DNS Suffix is already on the system
if Instr(sData, sNewData2) <> 0 then
' wsh.echo sNewdata2, "is already present"
else
if sData <> "" then sNewdata2 = "," & sNewdata2
'// Backup the current DNS Suffix List before any changes are made.
if sCreateBackup = TRUE then
.RegWrite sKey & sBackup, sData, "REG_SZ"
sCreateBackup = FALSE
end if
'// Write the DNS Suffix you want to add to the system
.RegWrite sKey & sValue, sData & sNewdata2, "REG_SZ"
' wsh.echo ".RegWrite " & skey & sValue, sData & sNewdata2, "REG_SZ"
end if
end with ' WSHShell
@ VBScab: I haven't been able to figure out why the code you posted would not work, but I'm sure I just need to spend a little more time on it and I should be able to figure it out.
@ bmonroe924: The code worked great, and I really liked how intuitive it was to read/understand it. There were two variable name errors, but I fixed that. In your example you only add one DNS Suffix, but I needed to add two, so I just repeat the code.
Dim sKey
Dim sValue
Dim sData
Dim sBackup
Dim sCreateBackup
Dim sNewData1
Dim sNewData2
sKey = "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\"
sValue = "SearchList"
sNewData1 = "abc.de.com"
sNewData2 = "fgh.ij.com"
sBackup = "Backup"
sCreateBackup = TRUE
with CreateObject("WScript.Shell")
on error resume next
'// Read the current DNS Suffix List
sData = .RegRead(sKey & sValue)
on error goto 0
'// Check to see if DNS Suffix is already on the system
if Instr(sData, sNewData1) <> 0 then
' wsh.echo sNewdata1, "is already present"
else
if sData <> "" then sNewdata1 = "," & sNewdata1
'// Backup the current DNS Suffix List before any changes are made.
'// Set sCreateBackup to FLASE so that a backup is not made each time a DNS Suffix is added
if sCreateBackup = TRUE then
.RegWrite sKey & sBackup, sData, "REG_SZ"
sCreateBackup = FALSE
end if
'// Write the DNS Suffix you want to add to the system
.RegWrite sKey & sValue, sData & sNewdata1, "REG_SZ"
' wsh.echo ".RegWrite " & skey & sValue, sData & sNewdata1, "REG_SZ"
end if
'// Read the current DNS Suffix List
sData = .RegRead(sKey & sValue)
on error goto 0
'// Check to see if DNS Suffix is already on the system
if Instr(sData, sNewData2) <> 0 then
' wsh.echo sNewdata2, "is already present"
else
if sData <> "" then sNewdata2 = "," & sNewdata2
'// Backup the current DNS Suffix List before any changes are made.
if sCreateBackup = TRUE then
.RegWrite sKey & sBackup, sData, "REG_SZ"
sCreateBackup = FALSE
end if
'// Write the DNS Suffix you want to add to the system
.RegWrite sKey & sValue, sData & sNewdata2, "REG_SZ"
' wsh.echo ".RegWrite " & skey & sValue, sData & sNewdata2, "REG_SZ"
end if
end with ' WSHShell
Posted by:
anonymous_9363
15 years ago
Posted by:
Nomi1985
15 years ago
@ VBScab: I took your advice. Not sure if this is what you meant, but it works. Let me know if you have any better suggestions. If there is a more elegant/better way of doing it, I would appreciate the comments.
Dim sKey
Dim sValue
Dim sData
Dim sBackup
Dim sCreateBackup
Dim aNewData(1) '// Create an array and set the size
Dim aPosition
Dim aLimit
sKey = "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\"
sValue = "SearchList"
sBackup = "Backup"
sCreateBackup = TRUE
aNewData(0) = "abc.de.com" '// Set the values in the array
aNewData(1) = "fgh.ij.com" '// Set the values in the array
aPosition = 0
aLimit = 2 '//Set the # of DNS Suffixes you are adding
with CreateObject("WScript.Shell")
Do While aPosition < aLimit
on error resume next
'// Read the current DNS Suffix List
sData = .RegRead(sKey & sValue)
on error goto 0
'// Check to see if DNS Suffix is already on the system
if Instr(sData, aNewData(aPosition)) <> 0 then
' wsh.echo aNewData(aPosition), "is already present"
else
if sData <> "" then aNewData(aPosition) = "," & aNewData(aPosition)
'// Backup the current DNS Suffix List before any changes are made.
'// Set sCreateBackup to FLASE so that a backup is not made each time a DNS Suffix is added
if sCreateBackup = TRUE then
.RegWrite sKey & sBackup, sData, "REG_SZ"
sCreateBackup = FALSE
end if
'// Write the DNS Suffix you want to add to the system
.RegWrite sKey & sValue, sData & aNewData(aPosition), "REG_SZ"
' wsh.echo ".RegWrite " & skey & sValue, sData & aNewData(aPosition), "REG_SZ"
end if
aPosition = aPosition + 1
Loop
end with ' WSHShell
Dim sKey
Dim sValue
Dim sData
Dim sBackup
Dim sCreateBackup
Dim aNewData(1) '// Create an array and set the size
Dim aPosition
Dim aLimit
sKey = "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\"
sValue = "SearchList"
sBackup = "Backup"
sCreateBackup = TRUE
aNewData(0) = "abc.de.com" '// Set the values in the array
aNewData(1) = "fgh.ij.com" '// Set the values in the array
aPosition = 0
aLimit = 2 '//Set the # of DNS Suffixes you are adding
with CreateObject("WScript.Shell")
Do While aPosition < aLimit
on error resume next
'// Read the current DNS Suffix List
sData = .RegRead(sKey & sValue)
on error goto 0
'// Check to see if DNS Suffix is already on the system
if Instr(sData, aNewData(aPosition)) <> 0 then
' wsh.echo aNewData(aPosition), "is already present"
else
if sData <> "" then aNewData(aPosition) = "," & aNewData(aPosition)
'// Backup the current DNS Suffix List before any changes are made.
'// Set sCreateBackup to FLASE so that a backup is not made each time a DNS Suffix is added
if sCreateBackup = TRUE then
.RegWrite sKey & sBackup, sData, "REG_SZ"
sCreateBackup = FALSE
end if
'// Write the DNS Suffix you want to add to the system
.RegWrite sKey & sValue, sData & aNewData(aPosition), "REG_SZ"
' wsh.echo ".RegWrite " & skey & sValue, sData & aNewData(aPosition), "REG_SZ"
end if
aPosition = aPosition + 1
Loop
end with ' WSHShell
Posted by:
Nomi1985
15 years ago
Here is another way of appending the DNS Suffix Search Order:
On Error Resume Next
Dim objNetworkSettings
Dim DicNewDNSSuffixSearchOrder
Dim arrNewDNSSuffixSearchOrder
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
'Define The Dictionary
Set DicNewDNSSuffixSearchOrder = CreateObject("Scripting.Dictionary")
' Go throu all NICs and get the DNS Suffix Order
For Each objNicConfig In colNicConfigs
If Not IsNull(objNicConfig.DNSDomainSuffixSearchOrder) Then
For Each strDNSSuffix In objNicConfig.DNSDomainSuffixSearchOrder
' Check if the Directory has that Value or not
If Not DicNewDNSSuffixSearchOrder.Exists(strDNSSuffix) Then _
DicNewDNSSuffixSearchOrder.Add strDNSSuffix,strDNSSuffix ' Add The DNS Suffix
Next
Exit For
End If
Next
' Check if the Directory has that Value or not
strDNSSuffix = "abc.de.com"
If Not DicNewDNSSuffixSearchOrder.Exists(strDNSSuffix) Then _
DicNewDNSSuffixSearchOrder.Add strDNSSuffix,strDNSSuffix ' Add The DNS Suffix
' Check if the Directory has that Value or not
strDNSSuffix = "fgh.ij.com"
If Not DicNewDNSSuffixSearchOrder.Exists(strDNSSuffix) Then _
DicNewDNSSuffixSearchOrder.Add strDNSSuffix,strDNSSuffix ' Add The DNS Suffix
' Get an Array of all The Items from the Dictionary
arrNewDNSSuffixSearchOrder = DicNewDNSSuffixSearchOrder.Items
' Set the New Array to the DNS Suffix Search Order
Set objNetworkSettings = _
objWMIService.Get("Win32_NetworkAdapterConfiguration")
intSetSuffixes = _
objNetworkSettings.SetDNSSuffixSearchOrder(arrNewDNSSuffixSearchOrder)
On Error Resume Next
Dim objNetworkSettings
Dim DicNewDNSSuffixSearchOrder
Dim arrNewDNSSuffixSearchOrder
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
'Define The Dictionary
Set DicNewDNSSuffixSearchOrder = CreateObject("Scripting.Dictionary")
' Go throu all NICs and get the DNS Suffix Order
For Each objNicConfig In colNicConfigs
If Not IsNull(objNicConfig.DNSDomainSuffixSearchOrder) Then
For Each strDNSSuffix In objNicConfig.DNSDomainSuffixSearchOrder
' Check if the Directory has that Value or not
If Not DicNewDNSSuffixSearchOrder.Exists(strDNSSuffix) Then _
DicNewDNSSuffixSearchOrder.Add strDNSSuffix,strDNSSuffix ' Add The DNS Suffix
Next
Exit For
End If
Next
' Check if the Directory has that Value or not
strDNSSuffix = "abc.de.com"
If Not DicNewDNSSuffixSearchOrder.Exists(strDNSSuffix) Then _
DicNewDNSSuffixSearchOrder.Add strDNSSuffix,strDNSSuffix ' Add The DNS Suffix
' Check if the Directory has that Value or not
strDNSSuffix = "fgh.ij.com"
If Not DicNewDNSSuffixSearchOrder.Exists(strDNSSuffix) Then _
DicNewDNSSuffixSearchOrder.Add strDNSSuffix,strDNSSuffix ' Add The DNS Suffix
' Get an Array of all The Items from the Dictionary
arrNewDNSSuffixSearchOrder = DicNewDNSSuffixSearchOrder.Items
' Set the New Array to the DNS Suffix Search Order
Set objNetworkSettings = _
objWMIService.Get("Win32_NetworkAdapterConfiguration")
intSetSuffixes = _
objNetworkSettings.SetDNSSuffixSearchOrder(arrNewDNSSuffixSearchOrder)
Posted by:
elgwhoppo
15 years ago
Thinking outside the scripting box, would you consider using a GPO?
Computer Configuration > Administrative Templates > Network > DNS Client > DNS Suffix Search List
Per the explanation:
Not trying to push you away from learning to use vbscript, it's an invaluable skill so I commend that. Just trying to make sure you are aware of the options.
Computer Configuration > Administrative Templates > Network > DNS Client > DNS Suffix Search List
Per the explanation:
If you enable this setting, you can specify the DNS suffixes to attach before submission of a query for an unqualified single-label name. The values of the DNS suffixes in this setting may be set using comma-separated strings, such as "microsoft.com,serverua.microsoft.com,office.microsoft.com". One DNS suffix is attached for each submission of a query. If a query is unsuccessful, a new DNS suffix is added in place of the failed suffix, and this new query is submitted. The values are used in the order they appear in the string, starting with the leftmost value and preceding to the right.
Not trying to push you away from learning to use vbscript, it's an invaluable skill so I commend that. Just trying to make sure you are aware of the options.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.