Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Network Adapter for Configuration 1

Status
Not open for further replies.

Rambeaux007

IS-IT--Management
May 4, 2005
23
US
Hello,

I am trying to create a script that will only configure the DNS suffixes for a specific adapter. Unfortunately I can only utilize the NetconnectionID name with happens to have a space in the name. Can someone point me in the right direction?

Thanks!!



On Error Resume Next

strComputer = ","
arrNewDNSSuffixSearchOrder = Array("domainA.net", "domainB.net",_
"domainC.net")

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE NetconnectionID = 'NIC One'")

For Each objNicConfig In colNicConfigs
strDNSHostName = objNicConfig.DNSHostName
Next
WScript.Echo VbCrLf & "DNS Host Name: " & strDNSHostName

For Each objNicConfig In colNicConfigs
WScript.Echo VbCrLf & _
" Network Adapter " & objNicConfig.Index & VbCrLf & _
" " & objNicConfig.Description & VbCrLf & _
" DNS Domain Suffix Search Order - Before:"
If Not IsNull(objNicConfig.DNSDomainSuffixSearchOrder) Then
For Each strDNSSuffix In objNicConfig.DNSDomainSuffixSearchOrder
WScript.Echo " " & strDNSSuffix
Next
End If
Next

WScript.Echo VbCrLf & String(80, "-")

Set objNetworkSettings = _
objWMIService.Get("Win32_NetworkAdapterConfiguration")
intSetSuffixes = _
objNetworkSettings.SetDNSSuffixSearchOrder(arrNewDNSSuffixSearchOrder)
If intSetSuffixes = 0 Then
WScript.Echo VbCrLf & "Replaced DNS domain suffix search order list."
ElseIf intSetSuffixes = 1 Then
WScript.Echo VbCrLf & "Replaced DNS domain suffix search order list." & _
VbCrLf & " Must reboot."
Else
WScript.Echo VbCrLf & "Unable to replace DNS domain suffix " & _
"search order list."
End If

WScript.Echo VbCrLf & String(80, "-")

Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration NetconnectionID = 'NIC One'")
For Each objNicConfig In colNicConfigs
WScript.Echo VbCrLf & _
" Network Adapter " & objNicConfig.Index & VbCrLf & _
" " & objNicConfig.Description & VbCrLf & _
" DNS Domain Suffix Search Order - After:"
If Not IsNull(objNicConfig.DNSDomainSuffixSearchOrder) Then
For Each strDNSSuffix In objNicConfig.DNSDomainSuffixSearchOrder
WScript.Echo " " & strDNSSuffix
Next
End If
Next
 
Query Win32_NetworkAdapter since it actually contains the NetConnectionID property...you can use the Like operator if you are running this on WinXP or better.

SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID LIKE 'NIC One%'

or if Nic One is the actual name

SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = 'NIC One'

Once you query for this, get the Index number and query Win32_NetworkAdapterConfiguration where the Index number matches the Index number retrieved above....you can then continue to use the rest of your script.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Do you mean like this? I don't think the quotes are correct because it doesn't seem to grab any NIC's.


Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = 'NIC One'",,48)
NICIndex = objItem.Index

Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration " & "Where Index = " & NICIndex,,48)

WScript.Echo VbCrLf & "Computer: " & strComputer

For Each objNicConfig In colNicConfigs
WScript.Echo VbCrLf & " Network Adapter " & objNicConfig.Index
WScript.Echo " DNS Server Search Order - Before:"
If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
For Each strDNSServer In objNicConfig.DNSServerSearchOrder
WScript.Echo " " & strDNSServer
Next
End If
intSetDNSServers = objNicConfig.SetDNSServerSearchOrder(arrNewDNSServerSearchOrder)
If intSetDNSServers = 0 Then
WScript.Echo " Replaced DNS server search order list."
Else
WScript.Echo " Unable to replace DNS server search order list."
End If
Next
 
Should be closer to this... (untested)

Code:
Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = 'NIC One'",,48)
For Each objNic In colNicConfigs
	NICIndex = objNic.Index
	WScript.Echo NICIndex
Next

Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration Where Index = '" & NICIndex & "'",,48)
 
WScript.Echo VbCrLf & "Computer: " & strComputer
 
For Each objNicConfig In colNicConfigs
  WScript.Echo VbCrLf & "  Network Adapter " & objNicConfig.Index
  WScript.Echo "    DNS Server Search Order - Before:"
  If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
    For Each strDNSServer In objNicConfig.DNSServerSearchOrder
      WScript.Echo "        " & strDNSServer
    Next
  End If
  intSetDNSServers = objNicConfig.SetDNSServerSearchOrder(arrNewDNSServerSearchOrder)
  If intSetDNSServers = 0 Then
    WScript.Echo "    Replaced DNS server search order list."
  Else
    WScript.Echo "    Unable to replace DNS server search order list."
  End If
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
This portion (below) seems to be skiping to "Unable to replace DNS..." Any ideas on this?

intSetDNSServers = objNicConfig.SetDNSServerSearchOrder(arrNewDNSServerSearchOrder)
If intSetDNSServers = 0 Then
WScript.Echo " Replaced DNS server search order list."
Else
WScript.Echo " Unable to replace DNS server search order list."
End If
 
I keep getting a 94 or 0 for the intSetDNSServers. But the code worked fine. I am still scratching my head on that one.

Thanks dm4ever!!
 
A 94 is "Path, file, or object not found." but I don't know why you'd be getting that.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
I seem to be getting a zero on one adapter then 94 on the rest. Any way to see what object it is looking for?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top