
也發個網絡設置相關批處理代碼代碼不是考慮很全面,權當參考,由于測試環境不全;代碼不免存在問題;望指正。
nclick="copycode($('code0'));">復制代碼
- @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

