通过bat文件修改win服务器远程桌面端口

通过bat文件修改windows sever服务器远程桌面端口号,不记得在哪里看到的了,希望能帮助到大家,将以下代码保存为bat,然后运行!

通过bat文件修改win服务器远程桌面端口

@echo off

rem 查找端口号
for /f "skip=4 tokens=3 delims= " %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v PortNumber') do (set value16=%%i)
set value=%value16:~2%
rem echo %value%
setlocal EnableDelayedExpansion
set "str=0123456789ABCDEF"
for /l %%a in (0 1 15) do (
 set -%%a=!str:~%%a,1!
 set -!str:~%%a,1!=%%a
)

for /l %%a in (0 1 100) do (
if not "!value:~%%a,1!"=="" (
call set sun=%%-!value:~%%a,1!%% !sun!
))
rem echo !sun!
set /a nnn=1,num=0
for %%a in (!sun!) do (
rem set num0=!num!
set /a num=%%a*nnn+num
rem echo num = %%a*!nnn! + !num0! = !num!
set /a nnn*=16
)
rem echo 当前远程桌面端口号为十六进制: %value16% 即 %value%
echo 修改后重启生效,记得把新端口加入防火墙.
rem setlocal DisableDelayedExpansion


rem 以下是系统默认
goto xiugai
@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\GloballyOpenPorts\List" /v 3389:TCP /t REG_SZ /d 3389:TCP:*:Enabled:@xpsp2res.dll,-22009 /f

:xiugai
set PortNumber=
set /p PortNumber=输入即将修改的十进制端口号(1024-65535):
rem if "%PortNumber%"=="" exit
if not defined PortNumber (set /a PortNumber=3389 & echo 未输入端口号,即将修改为默认值!PortNumber! & pause)
echo %PortNumber%|findstr "^[0-9]*$" >nul || (echo 必须纯数字! & goto xiugai)
if %PortNumber% lss 1024 (echo 端口号不能小于1024 & goto xiugai)
if %PortNumber% gtr 65535 (echo 端口号不能大于65535 & goto xiugai)
echo %PortNumber%

rem 修改端口后开启
rem 总开关
@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
rem 修改端口号,默认会直接修改十进制值,注意:范围在1024到65535,而且不能冲突,事实上,只修改第二处WinStations那一个也是可以的。

@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds\tcp" /v PortNumber /t REG_DWORD /d %PortNumber% /f
@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v PortNumber /t REG_DWORD /d %PortNumber% /f
@reg delete HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\GloballyOpenPorts\List /v !num!:TCP /f
@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\GloballyOpenPorts\List" /v %PortNumber%:TCP /t REG_SZ /d %PortNumber%:TCP:*:Enabled:@xpsp2res.dll,-22009 /f

转载请注明出处::【风酷seo网站优化排名公司】 » 通过bat文件修改win服务器远程桌面端口