Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

@echo off

set /p option=(1) Edit IP (2) Enable DHCP:

if %option%==1 (

set /p IP=New IP-Address:
set /p MASK=New Network Mask:
set /p GATE=New Gateway Address:

netsh interface ip set address name="LAN" static %IP% %MASK% %GATE% 1
)

if %option%==2 (

netsh interface ip set address name="LAN" source=dhcp
)

pause

The DHCP part of the program works just fine. The NIC's name is "LAN". I have tried with and without the 1 paramenter on the set IP. I have also tried with different variable names.

The error I get after entering a valid ipv4 address, a subnetmask and a gateway address is:

Invalid Address Parameter <1>: Must be a valid IPv4-Address

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
326 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your problem is delayed expansion. All variable reads inside a block of code (code inside parenthesis) are replaced with the value in the variable before the block starts to execute. If a variable is changed inside the block, that new value can not be retrieved, as all reads to variables were replaced with values. To solve, enable delayed expansion and, in variables where delayed read is needed, change %var% sintax with !var! to indicate the parser to delay the read until the time of execution.

@echo off

set /p option=(1) Edit IP (2) Enable DHCP:

if %option%==1 (
    set /p IP=New IP-Address:
    set /p MASK=New Network Mask:
    set /p GATE=New Gateway Address:

    setlocal enabledelayedexpansion
    netsh interface ip set address name="LAN" static !IP! !MASK! !GATE! 1
    endlocal
)

if %option%==2 (
    netsh interface ip set address name="LAN" source=dhcp
)

pause

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...