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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting DNS in Script 1

Status
Not open for further replies.

Ouch

Programmer
Jul 17, 2001
159
GB
Hi
i have come accross this code wich allows setting of ip details from a script

i need to set the DNS addresses as well any idea how i would do this? or where i could find out the syntax?


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strIPAddress = Array("192.168.1.141")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.100")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Next
 
Try:

myarray = array("192.168.1.141","255.255.255.0","192.168.1.100","1")
strIPAddress = myarray(0)
strSubnetMask = myarray(1)
strGateway = myarray(2)
strGatewayMetric = myarray(3)

The array function initializes an array with the values you supply. It cannot be used to create a dynamic array.

Myarray can be initialized as an array with the values you provided if you use the code above.

Hope this helps.

Dana
 
thanks Dana

i got it to work here is my code

this sets the machine TCPIP
IPAddress = 192.168.1.141
SubnetMask = 255.255.255.0
Gateway = 192.168.1.100
DNSServerSearchOrder = 192.168.129.250 & 192.168.129.251

it was the objNetAdapter.SetDNSServerSearchOrder i was missing took me a while to find it


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration_ where IPEnabled=TRUE")
strIPAddress = Array("192.168.1.141")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.100")
strGatewayMetric = Array(1)
strDNSServerSearchOrder = Array_("192.168.129.250","192.168.129.251")

For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNSServerSearchOrder)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Next
 
Would it be possibe to do the same thing for a dial up networking connection???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top