Search code examples
windowsbatch-filecmddnswmic

Script ignoring exit command and breaking out of function BATCH/CMD


This script I wrote is supposed to parse and pull the IP information out of WMIC. The issue is when it is used on a computer with only 1 DNS server entered, the script fails miserably. It appears to run the :remove function even if the variable DNS2 is not set and then the :remove function fails in some way I am not familiar with and ignores the exit command and breaks out of the function and into parts of the script I don't want it in. Here is the offending code, test it by setting your dns servers and index. Script works with 2 DNS servers defined and doesn't with only 1.

@echo off
setlocal enabledelayedexpansion
SETLOCAL ENABLEEXTENSIONS
wmic NICCONFIG where "Index = 1" get /Value | more > storage.txt

for /f "tokens=1-9 delims=,}{=" %%f in (storage.txt) do (
    set cat=%%f
    IF /i !cat!==IPAddress (set IP=%%g)
    IF /i !cat!==IPSubnet (set SUB=%%g)
    IF /i !cat!==DefaultIPGateway (set GW=%%g)
    IF /i !cat!==DNSServerSearchOrder (
        set DNS=%%g
        set DNS2=%%h
    )
    IF /i !cat!==IPEnabled (set enabled=%%g)
    IF /i !cat!==Description (set desc=%%g)
)
if /i %enabled%==True (
if defined IP (
    set item=IP
    CALL :remove
        )
if defined SUB (
    set item=SUB
    CALL :remove
        )
if defined GW (
    set item=GW
    CALL :remove
        )
if defined DNS (
    set item=DNS
    CALL :remove
        )
if defined DNS2 (
    set item=DNS2
    CALL :remove
        )
)
goto :menu
echo SHOULDN'T BE HERE
pause
:::::::::Function to remove quotes::::::::::::::
:remove
for /f tokens^=1^ delims^=^" %%p in (!%item%!) do (
    set !item!=%%p
    rem echo !%item%!
    EXIT /b
    )
echo I BROKE OUT OF THE FUNCTION
pause
:menu
echo I worked!
echo %IP%
echo %SUB%
echo %GW%
echo %DNS1%
if defined DNS2 ( echo %DNS2% )
pause

Here is another example using LotPings rework, note that even though there is not a DNS2, it still executes the bottom "if defined"

@echo off
SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion
Set Prop=IPAddress IPSubnet DefaultIPGateway DNSServerSearchOrder IPEnabled Description
for /f "tokens=1-9 delims=,}{=" %%f in (
  'wmic NICCONFIG where "Index = 1" get /Value^|findstr /i /B "%Prop%"'
) do (IF /i %%f==IPAddress set "IP=%%~g"
      IF /i %%f==IPSubnet  set "SUB=%%~g"
      IF /i %%f==DefaultIPGateway  set "GW=%%~g"
      IF /i %%f==DNSServerSearchOrder (
        set "DNS=%%~g"
        set "DNS2=%%~h"
      )
      IF /i %%f==IPEnabled   set "enabled=%%~g"
      IF /i %%f==Description set "desc=%%~g"
)
set|Findstr /i /B "IP SUB GW DNS Ena Desc"
echo I worked!
echo %DNS%
echo %DNS2%
if defined DNS2 ( echo DNS2 found, shouldn't have found this)
pause

Solution

  • EDIT Complete rework of the batch without temporary file:

    @echo off
    SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion
    Set Prop=IPAddress IPSubnet DefaultIPGateway DNSServerSearchOrder IPEnabled Description
    for /f "tokens=1-9 delims=,}{=" %%f in (
      'wmic NICCONFIG where "Index = 1" get /Value^|findstr /i /B "%Prop%"'
    ) do (IF /i %%f==IPAddress set "IP=%%~g"
          IF /i %%f==IPSubnet  set "SUB=%%~g"
          IF /i %%f==DefaultIPGateway  set "GW=%%~g"
          IF /i %%f==DNSServerSearchOrder (
            set "DNS=%%~g"
            set "DNS2=%%~h"
          )
          IF /i %%f==IPEnabled   set "enabled=%%~g"
          IF /i %%f==Description set "desc=%%~g"
    )
    If "%DNS2:~3%" EQU "" Set "DNS2="
    set|Findstr /i /B "IP SUB GW DNS Ena Desc"
    echo I worked!
    pause