我有一个在 WinXP SP3 上使用的批处理文件(我在网上找到了它并且它起作用了)。
切换到连接 1
@ECHO OFF 设置 varip=192.168.0.118 设置 varsm=255.255.255.0 设置 vargw=192.168.0.1 设置 vardns1=121.1.3.81 设置 vardns2=121.1.3.16
ECHO 设置 IP 地址和子网掩码 netsh int ip set address name = "本地连接" source = static addr = %varip% mask = %varsm%
ECHO 设置网关 netsh int ip set address name = "本地连接" gateway = %vargw% gwmetric = 1
ECHO 设置主 DNS netsh int ip set dns name = "本地连接" source = static addr = %vardns1%
ECHO 设置辅助 DNS netsh int ip add dns name = "本地连接" addr = %vardns2%
切换到连接 2
@ECHO OFF 设置 varip=192.168.0.118 设置 varsm=255.255.255.0 设置 vargw=192.168.0.2 设置 vardns1=192.168.0.2 设置 vardns2=192.168.0.8
ECHO 设置 IP 地址和子网掩码 netsh int ip set address name = "本地连接" source = static addr = %varip% mask = %varsm%
ECHO 设置网关 netsh int ip set address name = "本地连接" gateway = %vargw% gwmetric = 1
ECHO 设置主 DNS netsh int ip set dns name = "本地连接" source = static addr = %vardns1%
ECHO 设置辅助 DNS netsh int ip add dns name = "本地连接" addr = %vardns2%
不幸的是,这个批处理文件在我的 Win7(旗舰版)机器上不起作用。我想知道该编辑什么才能让它工作。我搜索了一些网站并尝试了一两种方法,但我找不到解决方案。非常感谢您的帮助。
答案1
您的批处理脚本似乎与其他网站上建议的脚本有很大不同,例如这或者这,所以我重写了你的批处理脚本。
我还将您的两个批处理脚本合并为一个,执行后,它将询问您要切换到哪个连接。
@echo off
::: Note ::::
echo Please make sure you are starting this batch script as an Administrator.
:::: Choice ::::
set /P c=Do you want to switch to connection one, or two [1/2]?
if /I "%c%" EQU "1" goto :ConnectionOne
if /I "%c%" EQU "2" goto :ConnectionTwo
goto :Exit
:::: Variables ::::
:ConnectionOne
set varip=192.168.0.118
set varsm=255.255.255.0
set vargw=192.168.0.1
set vardns1=121.1.3.81
set vardns2=121.1.3.16
goto :Start
:ConnectionTwo
set varip=192.168.0.118
set varsm=255.255.255.0
set vargw=192.168.0.2
set vardns1=192.168.0.2
set vardns2=192.168.0.8
goto:Start
:::: Program ::::
:Start
echo.
echo Setting IP Address [%varip%]
echo Setting Subnet Mask [%varsm%
echo Setting Gateway [%vargw%]
netsh interface ip set address name="Local Area Connection" static %varip% %varsm% %vargw%
echo Setting Primary DNS [%vardns1%]
echo Setting Secondary DNS [%vardns2%]
netsh interface ip set dns name="Local Area Connection" static %vardns1%
netsh interface ip add dns name="Local Area Connection" %vardns2% index=2
:Exit
pause
exit
请注意,即使上述批处理脚本是在 Windows 8.1 上编写和测试的,它也应该在 Windows 7 上同样有效。如果不行,请告诉我。