Enable Remote Desktop via cmdlet?

by nige-b at 2012-12-11 13:57:17

Hi,

Server 2012 & PowerShell 3.

I simply want to be able to allow remote desktop connections to a server (as done in the GUI via system properties->Remote Tab->Remote Desktop Options).

I have taken a look at the RemoteDesktop Module but cannot see a cmdlet to achieve this?

Is there one? If not any way to achieve via PowerShell?

Thanks,

Nigel.
by mikefrobbins at 2012-12-11 19:56:51
You can enable RDP with PowerShell by using the registry psprovider:

# Allow RDP Connections to this computer
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server' -Name fDenyTSConnections -Value 0
# Require Network Level Authentication
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name UserAuthentication -Value 1
You can use this to allow the firewall exception since you’re running PowerShell v3:

# Allow the Remote Desktop firewall exception
Set-NetFirewallRule -DisplayGroup 'Remote Desktop' -Enabled True
Since you’re using Server 2012 which means PowerShell remoting is enabled, you can throw all of this in the script block of Invoke-Command to remotely enable RDP:
Invoke-Command -ComputerName 'server1', 'server2' -ScriptBlock {
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server' -Name fDenyTSConnections -Value 0
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name UserAuthentication -Value 1
Set-NetFirewallRule -DisplayGroup 'Remote Desktop' -Enabled True
}