mirror of
https://github.com/bin456789/reinstall.git
synced 2025-12-10 07:28:56 +08:00
dd: 修复了用 wmic 获取网卡 id 时,!id! 变量保留了最后的 \r 导致语句不正确
fixes #444 fixes #445 fixes #446
This commit is contained in:
@ -12,72 +12,90 @@ rem set ipv6_dns2=::2
|
|||||||
|
|
||||||
@echo off
|
@echo off
|
||||||
mode con cp select=437 >nul
|
mode con cp select=437 >nul
|
||||||
setlocal EnableDelayedExpansion
|
|
||||||
|
|
||||||
rem 禁用 IPv6 地址标识符的随机化,防止 IPv6 和后台面板不一致
|
rem 禁用 IPv6 地址标识符的随机化,防止 IPv6 和后台面板不一致
|
||||||
netsh interface ipv6 set global randomizeidentifiers=disabled
|
netsh interface ipv6 set global randomizeidentifiers=disabled
|
||||||
|
|
||||||
rem 检查是否定义了 MAC 地址
|
rem 检查是否定义了 MAC 地址
|
||||||
if defined mac_addr (
|
if not defined mac_addr goto :del
|
||||||
rem vista 没有自带 powershell
|
|
||||||
rem win11 24h2 安装后有 wmic,但是过一段时间会自动删除,因此有的 dd 镜像没有 wmic
|
|
||||||
if exist "%windir%\system32\wbem\wmic.exe" (
|
|
||||||
for /f "tokens=2 delims==" %%a in (
|
|
||||||
'wmic nic where "MACAddress='%mac_addr%'" get InterfaceIndex /format:list ^| findstr /r "^InterfaceIndex=[0-9][0-9]*$"'
|
|
||||||
) do set id=%%a
|
|
||||||
)
|
|
||||||
if not defined id (
|
|
||||||
for /f %%a in ('powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass ^
|
|
||||||
-Command "(Get-WmiObject Win32_NetworkAdapter | Where-Object { $_.MACAddress -eq '%mac_addr%' }).InterfaceIndex" ^| findstr /r "^[0-9][0-9]*$"'
|
|
||||||
) do set id=%%a
|
|
||||||
)
|
|
||||||
if not defined id (
|
|
||||||
for /f %%a in ('powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass ^
|
|
||||||
-Command "(Get-CimInstance Win32_NetworkAdapter | Where-Object { $_.MACAddress -eq '%mac_addr%' }).InterfaceIndex" ^| findstr /r "^[0-9][0-9]*$"'
|
|
||||||
) do set id=%%a
|
|
||||||
)
|
|
||||||
if defined id (
|
|
||||||
rem 配置静态 IPv4 地址和网关
|
|
||||||
if defined ipv4_addr if defined ipv4_gateway (
|
|
||||||
rem gwmetric 默认值为 1,自动跃点需设为 0
|
|
||||||
netsh interface ipv4 set address !id! static !ipv4_addr! gateway=!ipv4_gateway! gwmetric=0
|
|
||||||
)
|
|
||||||
|
|
||||||
rem 配置静态 IPv4 DNS 服务器
|
rem vista 没有自带 powershell
|
||||||
for %%i in (1, 2) do (
|
rem win11 24h2 安装后有 wmic,但是过一段时间会自动删除,因此有的 dd 镜像没有 wmic
|
||||||
if defined ipv4_dns%%i (
|
if exist "%windir%\system32\wbem\wmic.exe" (
|
||||||
netsh interface ipv4 add | findstr "dnsservers"
|
rem wmic 换行符是 \r\r\n
|
||||||
if ErrorLevel 1 (
|
rem 虽然这里用了 findstr 全字匹配 ,但是结尾还是有 \r
|
||||||
rem vista
|
for /f "tokens=2 delims==" %%a in (
|
||||||
netsh interface ipv4 add dnsserver !id! !ipv4_dns%%i! %%i
|
'wmic nic where "MACAddress='%mac_addr%'" get InterfaceIndex /format:list ^| findstr "^InterfaceIndex=[0-9][0-9]*$"'
|
||||||
) else (
|
) do set id=%%a
|
||||||
rem win7
|
)
|
||||||
netsh interface ipv4 add dnsservers !id! !ipv4_dns%%i! %%i no
|
|
||||||
)
|
if not defined id (
|
||||||
|
for /f %%a in ('powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass ^
|
||||||
|
-Command "(Get-WmiObject Win32_NetworkAdapter | Where-Object { $_.MACAddress -eq '%mac_addr%' }).InterfaceIndex" ^| findstr "^[0-9][0-9]*$"'
|
||||||
|
) do set id=%%a
|
||||||
|
)
|
||||||
|
|
||||||
|
if not defined id (
|
||||||
|
for /f %%a in ('powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass ^
|
||||||
|
-Command "(Get-CimInstance Win32_NetworkAdapter | Where-Object { $_.MACAddress -eq '%mac_addr%' }).InterfaceIndex" ^| findstr "^[0-9][0-9]*$"'
|
||||||
|
) do set id=%%a
|
||||||
|
)
|
||||||
|
|
||||||
|
if defined id (
|
||||||
|
rem 配置静态 IPv4 地址和网关
|
||||||
|
if defined ipv4_addr if defined ipv4_gateway (
|
||||||
|
rem 如果使用了 setlocal EnableDelayedExpansion
|
||||||
|
rem netsh interface ipv4 set address !id! static %ipv4_addr% gateway=%ipv4_gateway% gwmetric=0
|
||||||
|
rem !id! 变量最后有 \r 会导致语句不正确
|
||||||
|
rem %id% 变量则没有这个问题
|
||||||
|
|
||||||
|
rem gwmetric 默认值为 1,自动跃点需设为 0
|
||||||
|
netsh interface ipv4 set address %id% static %ipv4_addr% gateway=%ipv4_gateway% gwmetric=0
|
||||||
|
)
|
||||||
|
|
||||||
|
rem 配置静态 IPv4 DNS 服务器
|
||||||
|
for %%i in (1, 2) do (
|
||||||
|
if defined ipv4_dns%%i (
|
||||||
|
netsh interface ipv4 add | findstr "dnsservers" >nul
|
||||||
|
if ErrorLevel 1 (
|
||||||
|
rem vista
|
||||||
|
setlocal EnableDelayedExpansion
|
||||||
|
netsh interface ipv4 add dnsserver %id% !ipv4_dns%%i! %%i
|
||||||
|
endlocal
|
||||||
|
) else (
|
||||||
|
rem win7
|
||||||
|
setlocal EnableDelayedExpansion
|
||||||
|
netsh interface ipv4 add dnsservers %id% !ipv4_dns%%i! %%i no
|
||||||
|
endlocal
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
rem 配置 IPv6 地址和网关
|
rem 配置 IPv6 地址和网关
|
||||||
if defined ipv6_addr if defined ipv6_gateway (
|
if defined ipv6_addr if defined ipv6_gateway (
|
||||||
netsh interface ipv6 set address !id! !ipv6_addr!
|
netsh interface ipv6 set address %id% %ipv6_addr%
|
||||||
netsh interface ipv6 add route prefix=::/0 !id! !ipv6_gateway!
|
netsh interface ipv6 add route prefix=::/0 %id% %ipv6_gateway%
|
||||||
)
|
)
|
||||||
|
|
||||||
rem 配置 IPv6 DNS 服务器
|
rem 配置 IPv6 DNS 服务器
|
||||||
for %%i in (1, 2) do (
|
for %%i in (1, 2) do (
|
||||||
if defined ipv6_dns%%i (
|
if defined ipv6_dns%%i (
|
||||||
netsh interface ipv6 add | findstr "dnsservers"
|
netsh interface ipv6 add | findstr "dnsservers" >nul
|
||||||
if ErrorLevel 1 (
|
if ErrorLevel 1 (
|
||||||
rem vista
|
rem vista
|
||||||
netsh interface ipv6 add dnsserver !id! !ipv6_dns%%i! %%i
|
setlocal EnableDelayedExpansion
|
||||||
) else (
|
netsh interface ipv6 add dnsserver %id% !ipv6_dns%%i! %%i
|
||||||
rem win7
|
endlocal
|
||||||
netsh interface ipv6 add dnsservers !id! !ipv6_dns%%i! %%i no
|
) else (
|
||||||
)
|
rem win7
|
||||||
|
setlocal EnableDelayedExpansion
|
||||||
|
netsh interface ipv6 add dnsservers %id% !ipv6_dns%%i! %%i no
|
||||||
|
endlocal
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
:del
|
||||||
rem 删除此脚本
|
rem 删除此脚本
|
||||||
del "%~f0"
|
del "%~f0"
|
||||||
|
|||||||
Reference in New Issue
Block a user