【发布时间】:2014-04-08 05:17:56
【问题描述】:
使用以下代码,我收到错误“无效的接口 LAN:指定”。此代码在 Win7 中有效,但在 Windows XP 中无效。我怀疑它与“!adapterName!”有关。在 XP 中发生冲突。
我利用这个脚本来获取 NIC 名称,以防它在未来发生一些变化并且希望保留这个方案。有什么想法可以在 XP 环境中继续使用此类脚本吗?
:: Set primary and alternate DNS for IPv4
@ECHO OFF
for /f "tokens=* delims=0" %%a in ("%SITEID%") DO set DROPZERO=%%a
SETLOCAL EnableDelayedExpansion
SET adapterName=
FOR /F "tokens=* delims=:" %%a IN ('IPCONFIG ^| FIND /I "ETHERNET ADAPTER"') DO SET adapterName=%%a
REM Removes "Ethernet adapter" from the front of the adapter name
SET adapterName=!adapterName:~17!
REM Removes the colon from the end of the adapter name
SET adapterName=!adapterName:~0,-1!
echo Setting Static IP Information...
set IP_Addr=10.102.%DROPZERO%.105
set D_Gate=10.102.%DROPZERO%.1
set Sub_Mask=255.255.255.0
netsh interface ip set address "!adapterName!" static %IP_Addr% %Sub_Mask% %D_Gate% 1 > output_net.txt
netsh interface ip set dns name="!adapterName!" static 10.98.1.26 primary >> output_net.txt
netsh interface ip add dns name="!adapterName!" 10.98.1.48 index=2 >> output_net.txt
【问题讨论】:
-
ipconfig输出的格式可能与 Windows 7 到 XP 不同。在 Windows 7 和 XP 中查看IPCONFIG ^| FIND /I "ETHERNET ADAPTER"的输出。如果格式不同,那么您的 for 循环可能无法正确设置adapterName。 -
我按照您的建议对其进行了分解,并且正确设置了适配器名称。
-
用
echo netsh替换你的netsh调用,看看这些命令是不是你认为的那样。 -
马克,谢谢!这把我的眼睛带到了他们需要的地方。这行代码 SET "adapterName=!adapterName:~0,-1!"实际需要是“adapterName=!adapterName:~0,-2!”注意 2. 删除适配器名称末尾的冒号。 -1 在 Win7 中工作,-2 在这种情况下需要。我已经验证了网卡的名称,也没有多余的空格。
-
既然你找到了解决办法,你能把它贴出来并标记为答案吗? :)
标签: batch-file set ip windows-xp nic