Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

VBScript for switching Static IPs to DHCP.

$
0
0

Hey everyone. Here's the quick run down:

We have about 200 machines that are scattered by different OS's (Windows XP, Windows 7 and a very few Windows 8) and they are all statically assigned. We would like to switch them over to DHCP without having to go to each machine and manually make them so.

I would like to get a script that would basically enumerate each physical LAN connection (Local Area Connection, 2, 3, 4, etc.) and set them from Static to DHCP. I have been playing with this script:

' *** This script will ONLY work on XP and up, 2000 and NT don't support the NetworkConnectionsID Property of the NetworkAdapter object
strComputer = "."
Set objShell = CreateObject("Wscript.Shell")

' Create WMI Service object
Set objWMIService = GetObject("winmgmts:" _& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

' Create a collection of all Adapters, that are bound to a Network Connection
Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapter Where (NOT NetConnectionID IS NULL)")

' A hack to try and not set a connection twice, even tho it won't hurt
CurrentID = """

' Loop through the collection of Adapters
For Each objNetworkAdapter in colNetAdapters
    ' If the adapter is different from the one we worked on before, do stuff
    If objNetworkAdapter.NetConnectionID <> CurrentID Then
        Wscript.Echo "netsh interface ip set address """ & objNetworkAdapter.NetConnectionID & """ source=dhcp"
        Wscript.Echo "netsh interface ip set dns """ & objNetworkAdapter.NetConnectionID & """ source=dhcp"
        Wscript.Echo "netsh interface ip set wins """ & objNetworkAdapter.NetConnectionID & """ source=dhcp"

' *********************************************
' Un Comment all this below to make it do stuff
' ********************************************* 
'	Wscript.Echo "Setting IP to DHCP"
'	Set oExecIP = objShell.Exec("netsh interface ip set address """ & objNetworkAdapter.NetConnectionID & """  source=dhcp")
'
'	Do While oExecIP.Status = 0
'	     WScript.Sleep 100
'	Loop
'
'	Wscript.Echo "Setting DNS to DHCP"
'	Set oExecDNS = objShell.Exec("netsh interface ip set dns """ & objNetworkAdapter.NetConnectionID & """  source=dhcp")
'
'	Do While oExecDNS.Status = 0
'   	 WScript.Sleep 100
'	Loop
'
'	Wscript.Echo "Setting WINS to DHCP"
'	Set oExecWINS = objShell.Exec("netsh interface ip set wins """ & objNetworkAdapter.NetConnectionID & """  source=dhcp")
'
'	Do While oExecWINS.Status = 0
'		WScript.Sleep 100
'	Loop
'
'	Set oExecIP = Nothing
'	Set oExecDNS = Nothing
'	Set oExecWINS = Nothing
' **********************************************
	' reset the currentID so we can check that the next one is different
        CurrentID = objNetworkAdapter.NetConnectionID
    End IF
Next

(I grabbed this script from the another site)

Here's the problem I run into. When I run the script on the computer, I see that it grabs the connection (For example, I changed "Local Area Connection" to "Local Area Connection 3") but it fails to actually switch it over to DHCP.

Could anyone help me shed some light on this particular issue I'm having?

Thanks,

Joe Neutrino.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>