#include #include Global Const $TCP = 6 Global Const $UDP = 17 ConsoleWrite(_RemotePort_Add(3380, $TCP) & @CRLF) Func _RemotePort_Add($iPort, $iType = 6) If StringRegExp($iPort, '^(6553[0-5]|655[0-2]\d|65[0-4]\d\d|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3})$') And StringRegExp($iType, '^[6]{1}|[17]{2}$') Then Local Const $sRegMainPath = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\WinStations" Local Const $asRegValueType[12] = ["REG_NONE", "REG_SZ", "REG_EXPAND_SZ", "REG_BINARY", "REG_DWORD", "REG_DWORD_BIG_ENDIAN", "REG_LINK", "REG_MULTI_SZ", "REG_RESOURCE_LIST", "REG_FULL_RESOURCE_DESCRIPTOR", "REG_RESOURCE_REQUIREMENTS_LIST", "REG_QWORD"] If Not StringInStr(_RegSearch("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\WinStations", $iPort, 4), "PortNumber = " & $iPort) Then For $i = 1 To 1000 Local $sRegMainPathValue = RegEnumVal($sRegMainPath & "\RDP-Tcp", $i) If @error <> 0 Then ContinueLoop EndIf If $sRegMainPathValue <> "PortNumber" Then Local $sRegRead = RegRead($sRegMainPath & "\RDP-Tcp", $sRegMainPathValue) Local $sRegType = $asRegValueType[@extended] If RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp-" & $iPort, $sRegMainPathValue, $sRegType, $sRegRead) = @error Then Return SetError(-3, 0, -3) EndIf EndIf Next If RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp-" & $iPort, "PortNumber", "REG_DWORD", $iPort) = @error Then Return SetError(-3, 0, -3) EndIf If $iType = $TCP Then If _AddPort("RDP Listening Port to Terminal Server", $iPort, 0, $TCP, "True") = @error Then Return SetError(-4, 0, -4) Else Return SetExtended(0, "SUCCESS: <" & $iPort & "> TCP port has been successfully opened!") EndIf Else If _AddPort("RDP Listening Port to Terminal Server", $iPort, 0, $UDP, "True") = @error Then Return SetError(-4, 0, -4) Else Return SetExtended(0, "SUCCESS: <" & $iPort & "> UDP port has been successfully opened!") EndIf EndIf Else Return SetExtended(0, 0) EndIf Else Return SetError(-1, 0, -1) EndIf EndFunc ;==>_RemotePort_Add