Pregunta

@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

¿Fue útil?

Solución

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top