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

Using Script to DHCP enable DNS preferre

Status
Not open for further replies.

keithvp

Instructor
Aug 10, 2000
19
US
I am using the script below to enable DHCP. However, it will not set the Preferred DNS server to DHCP enabled. I need to be able to switch between all static and all DHCP enabled. How do I add in the cabability to set the Preferred DNS as DHCP enabled after it has statically assigned IP addresses?

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableDHCP()
If errEnable = 0 Then
Wscript.Echo "DHCP has been enabled."
Else
Wscript.Echo "DHCP could not be enabled."
End If
Next

Thanks Keith
 
I know this is a VBS forum but I use 2 batch files to switch from my static home network and DHCP work place.
Place them in the winnt\system32 and use RUN from the start menu.

DHCP.BAT
netsh interface ip set address name="local area connection" source=dhcp
netsh interface ip set dns name="local area connection" source=dhcp

LAN.BAT
netsh interface ip set address name="local area connection" static 10.0.0.4 255.0.0.0 10.0.0.2 1
netsh interface ip set dns name="Local area connection" static 216.162.106.3
netsh interface ip add dns name="Local area connection" 216.162.106.4

They work on Windows 2000 and XP, I dont know about NT or 98.

Hope this helps.
Dan
 
This link has alot of helpful info on that subject


Here is some code that should work

strComputer = "."
Set wmisrv = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set NIC = wmisrv.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter in NIC
errEnable = objNetAdapter.EnableDhcp
errdns1 = objNetAdapter.SetDNSServerSearchOrder(dhcp)
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
 
Thanks. Both ways worked. However, I prefer the script.
Keith
 
Your right, the script runs faster. what script are you using to switch back to the static configuration? and does it include setting the proxy server settings in internet explorer?
 

I am using the following script for static configuration. However, I do not have anything for proxy. We do no use it.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strIPAddress = Array("172.16.20.227")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("172.16.20.249")
strGatewayMetric = Array(1)
strDNSServers=Array("172.16.20.38","172.16.20.4")
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errDNSSErvers = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed. Internet is available at Work."
Else
WScript.Echo "The IP address could not be changed. Internet is available at Work."
End If
Next

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top