Created
December 2, 2019 10:53
-
-
Save adiroiban/598c8bfc06f4a9d23492ed8dbcd73255 to your computer and use it in GitHub Desktop.
Change RDP port - Azure script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param($RDPPort=3389) | |
$TSPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server' | |
$RDPTCPpath = $TSPath + '\Winstations\RDP-Tcp' | |
Set-ItemProperty -Path $TSPath -name 'fDenyTSConnections' -Value 0 | |
# RDP port | |
$portNumber = (Get-ItemProperty -Path $RDPTCPpath -Name 'PortNumber').PortNumber | |
Write-Host Get RDP PortNumber: $portNumber | |
if (!($portNumber -eq $RDPPort)) | |
{ | |
Write-Host Setting RDP PortNumber to $RDPPort | |
Set-ItemProperty -Path $RDPTCPpath -name 'PortNumber' -Value $RDPPort | |
Restart-Service TermService -force | |
} | |
#Setup firewall rules | |
if ($portNumber -eq 3389) | |
{ | |
netsh advfirewall firewall set rule group="remote desktop" new Enable=Yes | |
} | |
else | |
{ | |
$systemroot = get-content env:systemroot | |
netsh advfirewall firewall add rule name="Remote Desktop - Custom Port" dir=in program=$systemroot\system32\svchost.exe service=termservice action=allow protocol=TCP localport=$RDPPort enable=yes | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment