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,
LVL 1
sqlagent007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MarkoBarkoCommented:
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
0
sr75Commented:
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
0
sqlagent007Author Commented:
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.
0
sr75Commented:
Yeah, you can do that or You could do what I did, I placed all my computers into an AD group so I didn't touch the systems that needed to have a static IP.

Here is the full script:

'On Error Resume Next

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

set ADSysInfo = Createobject("ADSystemInfo")
set Computer = GetObject("LDAP://" & ADSysInfo.ComputerName)
strgroup = LCase(Computer.MemberOF)

if inStr(strgroup, "computer-group-all-lower-case") then

      Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
            & strComputer & "\root\cimv2")
      strQuery = "Select * from Win32_NetworkAdapterConfiguration " & "where IPEnabled=TRUE"

      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
end if

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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.