有人可以帮助如何找出这段代码有什么问题吗?基本上,它只是我用来修复某些问题的脚本的一部分,我可以获得“DCHP Status”的结果,给出空白结果或无效答案,例如 dhcp lan 显示 NO。我无法在某些机器上使用 powershell,因此需要使用批处理。
基本上,代码使用 psexec 连接 LAN 上的远程计算机,并运行基于 DHCP 或静态 IP 的脚本并修复某些内容,以便我使用 sccm 远程正确连接。
@echo off
setlocal enabledelayedexpansion
:: Disabling UAC
echo Disabling UAC
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v "EnableLUA" /t REG_DWORD /d 0 /f >nul
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v "ConsentPromptBehaviorAdmin" /t REG_DWORD /d 0 /f >nul
:: Listing all network adapters
echo List all network adapters
netsh interface show interface
echo.
:: Getting the name of the network adapters
echo Enter the interface name you want to fix:
set /p adapterName=
:: Validating the user input
if not defined adapterName (
echo You must enter a network adapter name!
exit /b
)
:: Running ipconfig and storing the output in a temporary file
echo Run ipconfig and store the output in a temporary file
ipconfig > ipconfig.txt
REM ipconfig /all > dhcp.txt
netsh interface ip show addresses name="%adapterName%" > dhcp.txt
:: Parsing the IP address, subnet mask, and default gateway from the output
for /f "tokens=2 delims=:" %%A in ('findstr /i /c:"IPv4 Address" ipconfig.txt') do (
set "ip=%%A"
set "ip=!ip: =!"
)
for /f "tokens=2 delims=:" %%B in ('findstr /i /c:"Subnet Mask" ipconfig.txt') do (
set "mask=%%B"
set "mask=!mask: =!"
)
for /f "tokens=2 delims=:" %%C in ('findstr /i /c:"Default Gateway" ipconfig.txt') do (
set "gateway=%%C"
set "gateway=!gateway: =!"
)
for /f "tokens=2 delims=:" %%D in ('findstr /i /b /c:"DHCP Enabled" dhcp.txt') do (
set "DHCP=%%D"
set "DHCP=!DHCP: =!"
if /i "!DHCP!"=="Yes" (
set "DHCPStatus=Yes"
) else (
set "DHCPStatus=No"
)
)
if /i "%DHCPStatus%"=="Yes" (
goto :dhcp
) else (
goto :static
)
:dhcp
echo.
echo Actual network configuration of host
echo IP Address: !ip!
echo Subnet Mask: !mask!
echo Default Gateway: !gateway!
echo DHCP: !DHCPStatus!
echo.
:static
echo.
echo Actual network configuration of host
echo IP Address: !ip!
echo Subnet Mask: !mask!
echo Default Gateway: !gateway!
echo DHCP: !DHCPStatus!
echo.
整个代码:
@echo off && setlocal enabledelayedexpansion
set "_xptoKey=HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"
set "_ipv6Key=HKLM\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters"
set "_UACKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
echo/Disabling UAC & for %%i in =;( EnableLUA,ConsentPromptBehaviorAdmin
);= do >nul reg add "!_UACKey!" /v "%%~i" /t REG_DWORD /d 0 /f
:: Listing all network adapters interfaces
echo/List all network adapters & netsh interface show interface
:: Getting the name of the network adapters and validating the user input
echo/ & set /p "_adapterName=Enter the interface name you want to fix: " || =;(
echo You must enter a network adapter name! & exit /b );=
:: Parsing the IP address, subnet mask, and default gateway from the output
for /f "tokens=1-2 delims=:(" %%A in =;('
ipconfig /all ^| findstr /ic:"IPv4"
');= do set "_ip=%%~B" && call set "_ip=%%_ip: =%%"
for /f "tokens=1-2 delims=:(" %%A in =;('
ipconfig /all ^| findstr /ic:"Sub"
');= do set "_Sub=%%~B" && call set "_Sub=%%_Sub: =%%"
for /f "tokens=1-2 delims=:(" %%A in =;('
ipconfig /all ^| findstr /ic:"Gate"
');= do set "_Gate=%%~B" && call set "_Gate=%%_Gate: =%%"
:: Showing the current settings obtained
echo/Actual Network Configuration of Host
echo/ IP Address: !_ip!
echo/ Subnet Mask: !_sub!
echo/Default Gateway: !_gate!
echo/ DHCP: !_dhcp! & echo/
:: Applying configurations and executing common commands
echo/Disabling & =;(
echo/Disabling network adapter... Please Wait... >con
netsh interface set interface "Wi-Fi" disable
timeout /nobreak 6 | echo/ Enabling network adapter... Please wait... >con
netsh interface set interface "Wi-Fi" enable
timeout /nobreak 6 | echo/ >con
);= >nul 2>&1
:: Set the IP address, subnet mask, gateway, DNS servers if the current IP is not static
netsh interface ip show addresses name="!_adapterName!" | findstr "DHCP.*Yes" >nul || =;(
echo/Configuring the network settings...
netsh interface ipv4 set address name="!_adapterName!" source=static address="!_ip!" mask="!_sub!" gateway="!_gate!"
netsh interface ipv4 set dns name="!_adapterName!" static 1.1.1.1 validate=no
netsh interface ipv4 add dns name="!_adapterName!" 1.0.0.1 index=2 validate=no
netsh interface ipv4 delete dnsservers name="!_adapterName!" 1.2.3.4 validate=no
);=
echo/Adding DNS sufix: [xpto.pt + xpto2.pt]
reg add "!_xptoKey!" /f /v SearchList /t REG_SZ /d "xpto.pt,xpto2.pt" >nul
echo/Disable TCP/IP v6 for the network adapter...
reg add "!_ipv6Key!" /v "DisabledComponents" /t REG_DWORD /d 0xffffffff /f >nul
for %%i in =;( "set global randomizeidentifier=disabled","isatap set state state=disabled",
"6to4 set state state=disabled","set privacy state=disable","set teredo disable"
);= do netsh interface IPV6 %%~i
echo/Testing Network Connection (DHCP^)...
ping -n 4 10.0.25.80 | findstr "ms loss" | sort /Uniq
echo/Running Flushdns and Registerdns... & >nul =;(
ipconfig /flushdns & ipconfig /registerdns
);= 2>&1 | findstr "IP Cache" | Sort /Uniq
timeout /nobreak 5 | endlocal & goto :eof