Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4505
  • Last Modified:

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,
0
sqlagent007
Asked:
sqlagent007
  • 2
1 Solution
 
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

Featured Post

Industry Leaders: 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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now