Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript to Change the IP address WIFI

Status
Not open for further replies.

crackoo

Programmer
Feb 17, 2011
132
TN
Well I found this script in the Net to automatically change the static IP address of the WiFi network and the proxy address when I connect my laptop at work.
Well the script works fine except I now trying to make another script to put all in order when I connect from my home knowing that the address should be dynamic. So How can do that ?
Thanks !
Code:
' Initialize Objects
     Dim LocalClass_StdRegProv: Set LocalClass_StdRegProv = GetObject("winmgmts:{impersonationlevel=impersonate}!//./root/default:StdRegProv")
     Dim EmptyRecordset: Set EmptyRecordset = CreateObject("ADODB.Recordset")
 ' Define Constants
     Const HKEY_CLASSES_ROOT = &H80000000, HKCR = &H80000000
     Const HKEY_CURRENT_USER = &H80000001, HKCU = &H80000001
     Const HKEY_LOCAL_MACHINE = &H80000002, HKLM = &H80000002
     Const HKEY_USERS = &H80000003, HKU = &H80000003
     Const HKEY_PERFORMANCE_DATA = &H80000004, HKPD = &H80000004
     Const HKEY_CURRENT_CONFIG = &H80000005, HKCC = &H80000005
     Const HKEY_DYN_DATA = &H80000006, HKDD = &H80000006
     Const MaxCharacters = 255
     Const adVarChar = 200
     Const adInteger = 3
 ' Dimension Public Variables
     Dim EntryNumber: EntryNumber = 0
     Dim Return
 
 EmptyRecordset.Fields.Append "Entry", adInteger
 EmptyRecordset.Fields.Append "Name", adVarChar, MaxCharacters
 EmptyRecordset.Fields.Append "Server", adVarChar, MaxCharacters
 EmptyRecordset.Fields.Append "Port", adInteger
 EmptyRecordset.Open
 
 ' Add server/port
     EntryNumber = EntryNumber + 1
     EmptyRecordset.AddNew
     EmptyRecordset("Entry") = EntryNumber
     EmptyRecordset("Name") = "Proxy 172.16.0.1:3128"
     EmptyRecordset("Server") = "172.16.0.1"
     EmptyRecordset("Port") = 3128
     EmptyRecordset.Update
 
     EntryNumber = EntryNumber + 1
     EmptyRecordset.AddNew
     EmptyRecordset("Entry") = EntryNumber
     EmptyRecordset("Name") = "Sans Proxy"
     EmptyRecordset("Server") = ""
     EmptyRecordset("Port") = 80
     EmptyRecordset.Update
 
 
 Dim Message
 EmptyRecordset.MoveFirst
 Do While EmptyRecordset.EOF = False
     Message = Message & EmptyRecordset("Entry").Value & "." & vbTab & EmptyRecordset("Name").Value & vbCrLf
     EmptyRecordset.MoveNext
 Loop
 
 Do
     Dim intAnswer: intAnswer = InputBox(Message, "Choisir une puis validez sur Ok", "1")
     If IsNumeric(intAnswer) = True Then intAnswer = CLng(intAnswer)
     If intAnswer > EmptyRecordset.RecordCount Or intAnswer < 0 Then WScript.Echo "Invalid entry, please try again..."
 Loop Until (((VarType(intAnswer) And vbLong) = vbLong) And intAnswer <= EmptyRecordset.RecordCount And intAnswer >= 0)
 
 Select Case True
     Case (intAnswer = 0)
         WScript.Echo "Cancelled. Exiting."
     Case (((VarType(intAnswer) And vbLong) = vbLong) And intAnswer <= EmptyRecordset.RecordCount And intAnswer > 0)
         EmptyRecordset.Filter = "Entry=" & intAnswer
         Dim ProxyServer: ProxyServer = EmptyRecordset("Server").Value & ":" & EmptyRecordset("Port").Value
         intAnswer = MsgBox("Would you like to set the proxy to " & EmptyRecordset("Name").Value & " (" & ProxyServer & ")?", vbQuestion + vbYesNo + vbSystemModal, "Confirm")
         If intAnswer = vbYes Then
             Return = LocalClass_StdRegProv.SetDWORDValue(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyEnable", 1)
             Return = LocalClass_StdRegProv.SetStringValue(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer", ProxyServer)
             ChangeIP()
             If Err.Number = 0 And Return = 0 Then
                 WScript.Echo "Proxy settings changed. Exiting."
             Else
                 WScript.Echo "Proxy settings not changed. Exiting."
             End If
         Else
             WScript.Echo "No changes made. Exiting."
         End If
     Case Else
         WScript.Echo "Invalid entry. Exiting"
 End Select
 
 
 
Sub ChangeIP() 
strIP = Array("172.16.100.100")
strmask = Array("255.255.255.0")
 
strGateway = Array("")
strGatewayMetric = Array(1)
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
	
For Each objNetAdapter in colNetAdapters
    errEnable = objNetAdapter.EnableStatic(strIP, strmask)
    errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
    If errEnable = 0 Then
        WScript.Echo "The IP address has been changed."
    Else
        WScript.Echo "The IP address could not be changed."
    End If
Next
End Sub
 
This is from an HTA that allows me to change to 3 different configurations. I'm looking for "Broadcom" in the description to only change the wired NIC. You'll need to modify accordingly.
Code:
set colAdapterCfgs = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
...
sub SetDHCP()
	for each objAdapterCfg in colAdapterCfgs
		if InStr (1,objAdapterCfg.Caption,"Broadcom", 1) > 0 then
			errDHCP = objAdapterCfg.EnableDHCP()
			if errDHCP = 0 then
				Result.InnerText = "DHCP has been enabled."
				close_button.Focus
			else
				Result.InnerText = "DHCP could not be enabled."
			end if

			errDNS = objAdapterCfg.SetDNSServerSearchOrder()
			errDDNS = objAdapterCfg.SetDynamicDNSRegistration
			errRenew = objAdapterCfg.RenewDHCPLease

			if errRenew <> 0 then
				Result.InnerText = "DHCP lease could not be renewed." & err.number & err.description
			end if
		end if
	next
end sub

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Thank you for your reply ! i found a solution so here is my final script to reset my configuration with disabling the proxy and enabling the DHCP.
Code:
DesactiverProxy()
ActiverDHCP()

Sub DesactiverProxy()
Set ws = createObject("wscript.Shell")
ws.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0 , "REG_DWORD"
End Sub
 
 Sub ActiverDHCP() 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
	
For Each objNetAdapter in colNetAdapters
    errEnable = objNetAdapter.EnableDHCP()
    errGateways = objNetAdapter.SetGateways()
    If errEnable = 0 Then
        MsgBox "L'adresse IP a été bien changé et le DHCP est désormais Activé !" ,64, "Changer IP"
    Else
        MsgBox "L'adresse IP n'a pas été changé !" ,16, "Changer IP"
    End If
Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top