代码不是考虑很全面,权当参考,
由于测试环境不全;代码不免存在问题;望指正。- @echo off
- set "str=DefaultIPGateway^,IPAddress^,DNSServerSearchOrder^,IPSubnet^,DHCPEnabled"
-
- for /f "skip=1tokens=1*" %%i in ('wmic Nic where "Manufacturer<>'Microsoft' and NetConnectionStatus='2'" get Index^,NetConnectionID^') do (
- setlocal enabledelayedexpansion
- for /f %%j in ("%%j") do (
- set $%%i=%%j&echo 适配器 Index:[%%i] : 适配器名:"%%j"
- for /f "tokens=1-3delims={}," %%a in ('wmic Nicconfig where "Index='%%i'" get %str% /value') do (
- for /f %%c in (%%c) do if /i "%%a" == "DNSServerSearchOrder=" set "DNS2=%%c"
- for /f %%c in ("%%a%%~b") do set "%%c"
- )
- echo;
- if /i "!DHCPEnabled!" == "TRUE" (
- echo 获取IP地址模式:自动
- ) else echo 获取IP地址模式:手动
- echo ip地址: !IPAddress! & echo 子网掩码: !IPSubnet!
- echo 默认网关: !DefaultIPGateway! & echo 主DNS: !DNSServerSearchOrder!
- IF defined dns2 echo DNS2 :!DNS2!
- )
- endlocal
- )
- echo,
- :start
- set/pIndex=输入 适配器 对应 Index 退出 q
- if /i "%Index%" == "q" exit
- if not defined $%Index% goto start
-
- set/ps=输入 t 动态获取IP地址 f 手动获取IP地址
- goto %s%
-
- :t
- :动态获取IP地址
-
- wmic path Win32_NetworkAdapterConfiguration.index=%Index% call enabledhcp
- wmic path Win32_NetworkAdapterConfiguration.index=%Index% call SetDNSServerSearchOrder()
-
- pause & exit
-
- :f
- :手动获取IP地址
-
- set IP="192.168.0.10"
- set Mask="255.255.255.0"
- set Gateway="192.168.0.1"
- set DNS="192.168.0.1"
- wmic nicconfig where index=%Index% call enablestatic(%ip%),(%Mask%)
- wmic nicconfig where index=%Index% call setgateways(%Gateway%)
- wmic nicconfig where index=%Index% call setdnsdomain(%DNS%)
- wmic nicconfig where index=%Index% call SetDNSServerSearchOrder(%DNS%)
-
- pause
复制代码
|