脚本忽略退出命令并断开功能BATCH / CMD

时间:2017-01-18 22:06:00

标签: windows batch-file cmd dns wmic

我写的这个脚本应该解析并从WMIC中提取IP信息。问题是当它在只输入1个DNS服务器的计算机上使用时,脚本会失败。它似乎运行:remove函数,即使没有设置变量DNS2然后:remove函数以某种方式失败我不熟悉并忽略退出命令并打破函数并进入脚本的部分我不不想要它。这是有问题的代码,通过设置你的DNS服务器和索引来测试它。脚本适用于定义的2个DNS服务器,而不只有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

这是使用LotPings返工的另一个例子,请注意,即使没有DNS2,它仍然执行底部“如果已定义”

@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

3 个答案:

答案 0 :(得分:3)

编辑完成批量修改而不使用临时文件:

@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

答案 1 :(得分:0)

当您遇到此类问题时,应在任何相关点添加调试信息 在你的情况下添加一些echo

IF /i %%f==DNSServerSearchOrder (
        set "DNS=%%~g"
        set "DNS2=%%~h"
        echo #1 "%%~g"
        echo #2 "%%~h"
        if defined dns echo DNS defined
        if defined dns2 echo DNS2 defined
      )

我得到这样的东西

> #1 "10.51.16.9"
> "2 "
> DNS defined
> DNS2 defined

如您所见,DNS2已定义,echo #2 "%%~h"显示已销毁的行! 原因在于,wmic的行结尾是\r\r\n而不是\r\n 因此,在DNS2中,现在只有一个\r字符。

您可以使用清理功能从变量中删除所有\r 这有效,因为在通过百分号扩展的变量之后删除了所有\r(但是当它通过感叹号扩展或者如果它是%%p参数时不会被删除)

:sanitize
set "var=!%1!"
set "%1=%var%"
exit /b

并通过

使用它
IF /i %%f==DNSServerSearchOrder (
        set "DNS=%%~g"
        set "DNS2=%%~h"
        call :sanitize DNS
        call :sanitize DNS2
        echo #1 "!DNS!"
        echo #2 "!DNS2!"
        if defined dns echo DNS defined
        if defined dns2 echo DNS2 defined
      )

这就是......

答案 2 :(得分:0)

在解析for /F输出的那个周围换行另一个wmic循环:

rem // Code before the loop
for /F "delims=" %%e in ('
    wmic NICCONFIG where "Index = 1" get /Value ^| findstr /I /B "%Prop%"
') do (
    for /F "tokens=1-9 delims=,}{=" %%f in ("%%e") do (
        rem // Code inside of the loop...
    )
)
rem // Code after the loop...

wmic命令产生了Unicode输出,for /F循环没有正确转换为ANSI文本,还有一些孤立的回车字符,这会干扰解析。额外的for /F循环以一般且可靠的方式消除了这些伪影。