PowerShell custom Modify remote desktop RDP port

Source: Internet
Author: User

At the request of a friend, I wrote a script to modify the Remote Desktop through PowerShell. This script is not complicated. There are two options after the script is started: 1. custom Remote Desktop; 2. reply to the default port 3389 of Remote Desktop

The built-in cmdlets for Windows 8 and Windows 2012 are New-NetFirewallRule and Remove-NetFirewallRule.Therefore, this script is only applicable to Windows 8 and Windows and later versions., Pay attention to the applicable scope. If you want to be compatible with other versions of windows, replace the corresponding cmdlet with the netsh command. For how to use it, see the following help documentation http://technet.microsoft.com/library/hh831755.aspx

When modifying the RDP port, first modify the Registry "HKLM: \ SYSTEM \ CurrentControlSet \ Control \ Terminal Server \ WinStations \ RDP-Tcp the key value of "PortNumber" is the specified port, and then restart the Remote Desktop Services Service, add the firewall inbound request corresponding to the port to the firewall exception.

When restoring the default RDP port, first clear the previously added firewall exceptions, change the port with the corresponding key value in the Registry to 3389, and then restart the Remote Desktop Services Service.

It should be emphasized that during the restart of the Remote Desktop service, the Remote Desktop will be interrupted for a short time. You need to reconnect and add the firewall inbound requests corresponding to the port to the firewall exception, only the firewall at the operating system level is added here. If you are using a virtualization solution similar to Windows Azure, you must add an External Firewall exception, otherwise, the modified RDP port cannot be connected from outside.

Not much nonsense. Go to the Code:

Clear
Write-Host
Write-Host 1, custom Remote Desktop port-ForegroundColor 10
Write-Host 2. Restore the system's default Remote Desktop port-ForegroundColor 11
Write-Host
Write-Host
Write-Host "select an option from the above list... [1-2]"
$ Opt = Read-Host
Switch ($ opt)
{
1 {
Write-Host
Write-Host modify the default port of Remote Desktop)...-ForegroundColor Red
Write-Host
Write-Host will prompt you to enter the port number to be specified. Please refer to the port range to enter a specified port number range: 1024 ~ 65535)
Write-Host: Modify the key value of PortNumber in the Registry "HKLM: \ SYSTEM \ CurrentControlSet \ Control \ Terminal Server \ WinStations \ RDP-Tcp.
Write-Host
# Enter the specified port number and modify the RDP default port
$ PortNumber = Read-Host "enter the port number range to be specified: 1024 ~ 65535 )"
$ Original = Get-ItemProperty-Path 'hklm: \ SYSTEM \ CurrentControlSet \ Control \ Terminal Server \ WinStations \ RDP-Tcp '-name' portnumber'
Write-Host the current RDP default port is $ original. PortNumber
$ Result = Set-ItemProperty-Path 'hklm: \ SYSTEM \ CurrentControlSet \ Control \ Terminal Server \ WinStations \ RDP-Tcp '-Name 'portnumber'-Value $ portnumber
If ($ result. PrimaryStatus-eq 'OK ')
{
Write-Host has completed RDP port modification! -ForegroundColor Green
}
Else
{
Failed to modify RDP port on Write-Host! -ForegroundColor Red
}
# Restart the remote desktop service
Write-Host is restarting Remote Desktop Services...-ForegroundColor DarkYellow
Restart-Service termservice-Force
# Allow custom ports to pass through the firewall
Write-Host: Add a firewall policy to allow the existing RDP port $ PortNumber to enter the site.
$ Result = New-NetFirewallRule-DisplayName "Allow Custom RDP PortNumber"-Direction Inbound-Protocol TCP-LocalPort $ PortNumber-Action Allow
If ($ result. PrimaryStatus-eq 'OK ')
{
Write-Host has completed adding the firewall policy corresponding to the RDP port! -ForegroundColor Green
}
Else
{
Failed to add the firewall policy corresponding to the RDP port on the Write-Host! -ForegroundColor Red
}
Write-Host
Write-Host completes RDP port modification!
}
2 {
Write-Host
The Write-Host is restoring the default port...
Set-ItemProperty-Path 'hklm: \ SYSTEM \ CurrentControlSet \ Control \ Terminal Server \ WinStations \ RDP-Tcp '-Name 'portnumber'-Value 3389
The Write-Host is restarting Remote Desktop Services...
Restart-Service termservice-Force
Write-Host is deleting firewall settings...
Remove-NetFirewallRule-DisplayName "Allow Custom RDP PortNumber"
The write-host is restored!
}
}

The following figure shows how to modify the RDP port:

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px "title =" image "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/001S12111-0.png "width =" 820 "height =" 338 "/>

Shows how to restore the default RDP port:

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/001S11L9-1.png "width =" 816 "height =" 240 "/>

 

Author: Fu haijun
Source: http://fuhj02.blog.51cto.com
Copyright: The copyright of this article is owned by the author and 51cto.
Reprinted: you are welcome to reprinted. Please Reprinted as required to save the author's Creative Enthusiasm.] Thank you.
Requirement: This statement must be retained without the author's consent. The original Article must be connected and the content must be complete! Otherwise, legal liability is required!
Personal Website: http://www.fuhaijun.com/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.