gpt4 book ai didi

windows - 脚本忽略退出命令并中断函数 BATCH/CMD

转载 作者:可可西里 更新时间:2023-11-01 14:14:22 25 4
gpt4 key购买 nike

我编写的这个脚本应该解析 IP 信息并将其从 WMIC 中提取出来。问题是当它在只输入 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,它仍然执行底部“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

最佳答案

编辑 在没有临时文件的情况下完成批处理的返工:

@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

关于windows - 脚本忽略退出命令并中断函数 BATCH/CMD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41730294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com