Link to home
Start Free TrialLog in
Avatar of sqlagent007
sqlagent007Flag for United States of America

asked on

Write a VBScript to change the Static IP setting to DHCP

I have a list of hostnames that have Static IP addresses, i would like to get \ write a VBScript that will change the setting on the clinets to DHCP.

Thanks in advance,
Avatar of MarkoBarko
MarkoBarko

Take a look at WMI in the MSDN docs...I know that there is a method EnableStatic to do it locally. I don't know if there is a way to do it remotely. Maybe there is a way to log on to the remote machine and then execute these commands? Maybe others can fill this in with more info...

WMI reference:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_reference.asp

EnableStatic method:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_reference.asp
This did it for me:

Set Net = Wscript.CreateObject("Wscript.Network")

strComputer = ucase(net.computername)
strWMI = "winmgmts:\\" & strComputer & "\root\cimv2"
strQuery = "Select * from Win32_NetworkAdapterConfiguration " & "where IPEnabled=TRUE"

Set objWMIService = GetObject(strWMI)
Set colNetAdapters = objWMIService.ExecQuery(strQuery)
 
For Each objNetAdapter In colNetAdapters
      errEnable = objNetAdapter.EnableDHCP()
      errDNS = objNetAdapter.SetDNSServerSearchOrder(null)
      errWINS = objNetAdapter.SetWINSServer("", "")
      RemoveGTW strComputer, objNetAdapter.SettingID
Next


Sub RemoveGTW(strComputer, lsnicsid)
      Const HKEY_LOCAL_MACHINE = &H80000002
      arrStringValues = Array()
      strEntryName = "DefaultGateway"

      lsStrWMI = "winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv"
      Set objReg=GetObject(lsStrWMI)

      strKeyPath = "SYSTEM\CurrentControlSet\Services\" & lsNicSid & "\Parameters\Tcpip\"
      objReg.SetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, strEntryName,arrStringValues

      strKeyPath = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" & lsNicSid
      objReg.SetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, strEntryName,arrStringValues

End Sub
Avatar of sqlagent007

ASKER

sr75, is there a way i can have this script read from a text file for the computer name value?

strComputer = ucase(net.computername)

I would like to create a text file with the 100 or so hostnames, then just have the script go one by one down the list.

Is this possible?

Thanks very much.
ASKER CERTIFIED SOLUTION
Avatar of sr75
sr75
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial