?
Solved

Write a VBScript to change the Static IP setting to DHCP

Posted on 2005-03-29
4
Medium Priority
?
4,441 Views
Last Modified: 2008-01-09
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,
0
Comment
Question by:sqlagent007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:MarkoBarko
ID: 13655137
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
 
LVL 15

Expert Comment

by:sr75
ID: 13655332
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
 
LVL 1

Author Comment

by:sqlagent007
ID: 13657195
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
 
LVL 15

Accepted Solution

by:
sr75 earned 2000 total points
ID: 13661605
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Simple Linear Regression
Introduction to Processes
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question