Jump to content

Recommended Posts

Posted

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 it

below the code i've just written :

setlocal enabledelayedexpansion

title "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 lot

PS : Sorry for my poor english


Posted

You have 2 different scripting langauges in your example, thet are Cmd Promt and VBS script.

Try this vbs script

Dim 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
Next

As to your Cmd Promt problem to get a user input

@Echo Off
CLS


:Main
Title Main Menu
MODE 62,15
COLOR 9F

Echo.
Echo Type 1 To Use Wireless broadcom
Echo.
Echo Type 2 To Use Ethernet Gigabyte adpater
Echo.
Echo Type 3 To Use EThernet Broadcom3 adpater
Echo.
SET UC=
SET /P UC=Type In Your Reply Then Press Enter ^>
IF /I '%UC%'=='1' GOTO Choice1
IF /I '%UC%'=='2' GOTO Choice2
IF /I '%UC%'=='3' GOTO Choice3

::Makes Sure User Select AN Valid Option
CLS
MODE 55,5
COLOR F4
TITLE Input Error
ECHO.
ECHO This was not valid ^> %UC%
ping -n 3 127.0.0.1>nul
GOTO Main

:Choice1
Title Choice 1
CLS
Mode 62, 7
Color 9E
Echo.
Echo You selected %UC%
pause
Exit

:Choice2
Title Choice 2
CLS
Mode 62, 7
Color 9E
Echo.
Echo You selected %UC%
pause
Exit

:Choice3
Title Choice 3
CLS
Mode 62, 7
Color 9E
Echo.
Echo You selected %UC%
pause
Exit

Posted (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 by Mickmix69
Posted

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 Sub
End Class

Use this Source Code

You will need to installl Vb,net to open this project you can Download it at this Link

ipToolImg.png

Posted

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"
main
Sub Main
dim strComputer
dim Carte(50)
dim saisie
dim i, j, a
on error resume next
strComputer = "."

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 = 1

For Each objNetAdapter in colNetAdapters
carte(i) = objNetAdapter.caption
i = i + 1
Next
i = i - 1

for j = 1 to i
Carte(0) = carte(0) & j & " --> " & carte(j) & chr(13) & chr(13)
next
carte(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 if
ChoixConfig = 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 main

If 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 If
end if
If 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 If
end if
end sub

Thank you for your reactivity ;)

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...