Mickmix69 Posted March 9, 2009 Posted March 9, 2009 HEllo all,I'm writting a batch file to allow IP configuration change. I use netsh :(netsh interface ip set address "EThernet" static 192.168.11.10 255.255.255.0)My problem is : I have to know the network adapter name. So if it's wrong I'd like To change it by selecting it in a list (in a dos window) for exemple :------------------------------------------------list of network adapter name : 1 - Wireless broadcom....2- Ethernet Gigabyte adpater....3- EThernet Broadcom3 adpater....What is your choice ? (enter the number of the network adapter)-----------------------------------------------I think I should use the Win32_NetworkAdapter class but I don't know how i ould use itbelow the code i've just written :setlocal enabledelayedexpansiontitle "Changer la configuration IP"echo Changement de la Configuration IP vers la config FSU............netsh interface ip set address "Ethernet" static 192.168.11.10 255.255.255.0 if errorlevel 1 ( echo Problem, select the network adapter rem strComputer = "." rem Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter") pause For Each objItem in colItems Wscript.Echo "Name: " & objItem.Name Wscript.Echo Next set /p Your choice : netsh interface ip set address objItem.Name static 192.168.11.10 255.255.255.0 )Thanks a lotPS : Sorry for my poor english
gunsmokingman Posted March 9, 2009 Posted March 9, 2009 You have 2 different scripting langauges in your example, thet are Cmd Promt and VBS script.Try this vbs scriptDim Wmi :Set Wmi = GetObject("winmgmts:\\.\root\CIMV2") Dim Obj For Each Obj in Wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapter") If InStr(LCase(Obj.Name),LCase("ethernet")) Or InStr(LCase(Obj.Name),LCase("Wireless")) Then Wscript.Echo "Name: " & Obj.Name & vbCrLf End if NextAs to your Cmd Promt problem to get a user input@Echo OffCLS:MainTitle Main MenuMODE 62,15COLOR 9FEcho.Echo Type 1 To Use Wireless broadcomEcho.Echo Type 2 To Use Ethernet Gigabyte adpaterEcho.Echo Type 3 To Use EThernet Broadcom3 adpaterEcho.SET UC=SET /P UC=Type In Your Reply Then Press Enter ^> IF /I '%UC%'=='1' GOTO Choice1IF /I '%UC%'=='2' GOTO Choice2IF /I '%UC%'=='3' GOTO Choice3::Makes Sure User Select AN Valid OptionCLSMODE 55,5COLOR F4TITLE Input ErrorECHO.ECHO This was not valid ^> %UC%ping -n 3 127.0.0.1>nulGOTO Main:Choice1Title Choice 1CLSMode 62, 7Color 9EEcho.Echo You selected %UC%pauseExit:Choice2Title Choice 2CLSMode 62, 7Color 9EEcho.Echo You selected %UC%pauseExit:Choice3Title Choice 3CLSMode 62, 7Color 9EEcho.Echo You selected %UC%pauseExit
Mickmix69 Posted March 9, 2009 Author Posted March 9, 2009 (edited) Ok, thanks a lot.It works but separately. I think i'm going tu use C to do this. I don't know enough vbs or bat languages.I will use the vbscript idea in the C code. If it works i will post it ...Thank you ^^ Edited March 9, 2009 by Mickmix69
gunsmokingman Posted March 9, 2009 Posted March 9, 2009 Here I wrote this VB.net 2008 app it should do what you want.Public Class Form1 '-> Fill The Listbox OnLoad Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each Obj In GetObject("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_NetworkAdapter") If InStr(LCase(Obj.Name), LCase("ethernet")) Or InStr(LCase(Obj.Name), LCase("Wireless")) Then ListBox1.Items.Add(Obj.Name) End If Next End Sub Dim Chk1, Chk2 As Boolean '-> Listbox1 Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Chk1 = True End Sub '-> Textbox1 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Chk2 = True End Sub '-> Button 1 Click Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Chk1 = True And Chk2 = True And Len(TextBox1.Text) >= 7 Then If InStr(TextBox1.Text, ".") Then Dim NetCard = ListBox1.SelectedItem Dim IpAddress = TextBox1.Text Microsoft.VisualBasic.CreateObject("Wscript.Shell").Run _ ("netsh interface ip set address " & NetCard & " static " & IpAddress, 0, True) End If ElseIf Chk1 = False And Chk2 = False Then MsgBox(vbTab & "Error" & vbCrLf & "You Must Select An Network Card" & vbCrLf & _ "There WasNo Valid IP Was Type In") ElseIf Chk1 = True And Chk2 = True And Len(TextBox1.Text) <= 6 Then MsgBox("Error This Is Not A Valid IP Address") Else MsgBox("Error Some Thing Happen And I Dont Know What") End If End SubEnd ClassUse this Source CodeYou will need to installl Vb,net to open this project you can Download it at this Link
Mickmix69 Posted March 10, 2009 Author Posted March 10, 2009 Thank you i will use it sure!However, I succeed to write a vbs script in order to list network adapter and to change a pre-defined IP configuration : Msgbox "Cette application permet de switcher la config IP de la carte Réseau Ethernet" & chr(13) & _"entre l'adressage par DHCP et un adressage statique Compatible avec le FSU." & chr(13) & chr(13) & _"Version de soft : 1.0.1" & chr(13) & "Auteur : Michael VALLET", vbinformation, "Infos"mainSub Maindim strComputerdim Carte(50)dim saisiedim i, j, aon error resume nextstrComputer = "."Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colNetAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")strIPAddress = Array("192.168.11.10")strSubnetMask = Array("255.255.255.0")'strGateway = Array("192.168.5.190")'strDNSServers = Array("97.20.255.254")strGatewayMetric = Array(1)carte(0) = ""i = 1For Each objNetAdapter in colNetAdapters carte(i) = objNetAdapter.caption i = i + 1Nexti = i - 1for j = 1 to i Carte(0) = carte(0) & j & " --> " & carte(j) & chr(13) & chr(13)nextcarte(0) = carte(0) & chr(13) & chr(13) & "Taper E pour ouvrir directement l'explorateur FSU"saisie = inputbox(carte(0),"Choix de la carte Réseau") if saisie = "" then exit sub 'if saisie > i then main if saisie = "E" or saisie = "e" then Set WshShell = CreateObject("WScript.Shell") Set oExec = WshShell.Exec("explorer \\192.168.11.11\Dossier_partager") exit sub end ifChoixConfig = inputbox("Quelle config voulez vous charger?" & chr(13) & chr(13) & "1 --> DHCP Alstom" & chr(13) & "2 --> FSU","Choix de la Config IP") if ChoixConfig = "" then exit sub if choixConfig > 2 then mainIf choixConfig = "1" then For Each objNetAdapter in colNetAdapters if objNetAdapter.caption = carte(saisie) then 'msgbox objNetAdapter.caption & " ok, on rentre dans le IF" errEnable = objNetAdapter.EnableDHCP 'EnableStatic(strIPAddress, strSubnetMask) 'errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) 'errDOM = objNetAdapter.EnableDNS'SetDNSServerSearchOrder(strDNSServers) end if next if errEnable = 0 Then WScript.Echo "L'addresse IP a été changée correctement." Else WScript.Echo "L'addresse n'a pas été changée il y a un Pb." End Ifend ifIf choixConfig = "2" then For Each objNetAdapter in colNetAdapters if objNetAdapter.caption = carte(saisie) then WScript.Echo "L'ordinateur se connecte. Veuillez quelques secondes...." errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) 'errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) 'errDOM = objNetAdapter.EnableDNS'SetDNSServerSearchOrder(strDNSServers) end if next if errEnable = 0 Then WScript.Echo "L'addresse IP a été changée correctement." Set WshShell = CreateObject("WScript.Shell") Set oExec = WshShell.Exec("explorer \\192.168.11.11\Dossier_partager") Else WScript.Echo "L'addresse n'a pas été changée il y a un Pb." End Ifend ifend subThank you for your reactivity
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now