This script that I wrote should parse and extract IP information from WMIC. The problem is that when it is used on a computer with only one DNS server input, the script fails. It seems that it runs the function: remove, even if the DNS2 variable is not set, and then: the remove function does not work, I am not familiar and ignore the exit command and breaks out of the function and in the script part I do not want this. Here is the violation code, check it by installing your DNS servers and index. The script works with two DNS servers and does not have 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 of using LotPings refinement, note that even though there is no DNS2, it still does 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