1+ # PowerShell script to configure RDP for Coder Desktop access
2+ # This script enables RDP, sets the admin password, and configures necessary settings
3+
4+ Write-Output " [Coder RDP Setup] Starting RDP configuration..."
5+
6+ # Function to set the administrator password
7+ function Set-AdminPassword {
8+ param (
9+ [string ]$adminUsername ,
10+ [string ]$adminPassword
11+ )
12+
13+ Write-Output " [Coder RDP Setup] Setting password for user: $adminUsername "
14+
15+ try {
16+ # Convert password to secure string
17+ $securePassword = ConvertTo-SecureString - AsPlainText $adminPassword - Force
18+
19+ # Set the password for the user
20+ Get-LocalUser - Name $adminUsername | Set-LocalUser - Password $securePassword
21+
22+ # Enable the user account (in case it's disabled)
23+ Get-LocalUser - Name $adminUsername | Enable-LocalUser
24+
25+ Write-Output " [Coder RDP Setup] Successfully set password for $adminUsername "
26+ } catch {
27+ Write-Error " [Coder RDP Setup] Failed to set password: $_ "
28+ exit 1
29+ }
30+ }
31+
32+ # Function to enable and configure RDP
33+ function Enable-RDP {
34+ Write-Output " [Coder RDP Setup] Enabling Remote Desktop..."
35+
36+ try {
37+ # Enable RDP
38+ Set-ItemProperty - Path ' HKLM:\System\CurrentControlSet\Control\Terminal Server' - Name " fDenyTSConnections" - Value 0 - Force
39+
40+ # Disable Network Level Authentication (NLA) for easier access
41+ Set-ItemProperty - Path ' HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' - Name " UserAuthentication" - Value 0 - Force
42+
43+ # Set security layer to RDP Security Layer
44+ Set-ItemProperty - Path ' HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' - Name " SecurityLayer" - Value 1 - Force
45+
46+ Write-Output " [Coder RDP Setup] RDP enabled successfully"
47+ } catch {
48+ Write-Error " [Coder RDP Setup] Failed to enable RDP: $_ "
49+ exit 1
50+ }
51+ }
52+
53+ # Function to configure Windows Firewall for RDP
54+ function Configure-Firewall {
55+ Write-Output " [Coder RDP Setup] Configuring Windows Firewall for RDP..."
56+
57+ try {
58+ # Enable RDP firewall rules
59+ Enable-NetFirewallRule - DisplayGroup " Remote Desktop" - ErrorAction SilentlyContinue
60+
61+ # If the above fails, try alternative method
62+ if ($LASTEXITCODE -ne 0 ) {
63+ netsh advfirewall firewall set rule group= " remote desktop" new enable= Yes
64+ }
65+
66+ Write-Output " [Coder RDP Setup] Firewall configured successfully"
67+ } catch {
68+ Write-Warning " [Coder RDP Setup] Failed to configure firewall rules: $_ "
69+ # Continue anyway as RDP might still work
70+ }
71+ }
72+
73+ # Function to ensure RDP service is running
74+ function Start-RDPService {
75+ Write-Output " [Coder RDP Setup] Starting Remote Desktop Services..."
76+
77+ try {
78+ # Start the Terminal Services
79+ Set-Service - Name " TermService" - StartupType Automatic - ErrorAction SilentlyContinue
80+ Start-Service - Name " TermService" - ErrorAction SilentlyContinue
81+
82+ # Start Remote Desktop Services UserMode Port Redirector
83+ Set-Service - Name " UmRdpService" - StartupType Automatic - ErrorAction SilentlyContinue
84+ Start-Service - Name " UmRdpService" - ErrorAction SilentlyContinue
85+
86+ Write-Output " [Coder RDP Setup] RDP services started successfully"
87+ } catch {
88+ Write-Warning " [Coder RDP Setup] Some RDP services may not have started: $_ "
89+ # Continue anyway
90+ }
91+ }
92+
93+ # Main execution
94+ try {
95+ # Template variables from Terraform
96+ $username = " ${username} "
97+ $password = " ${password} "
98+
99+ # Validate inputs
100+ if ([string ]::IsNullOrWhiteSpace($username ) -or [string ]::IsNullOrWhiteSpace($password )) {
101+ Write-Error " [Coder RDP Setup] Username or password is empty"
102+ exit 1
103+ }
104+
105+ # Execute configuration steps
106+ Set-AdminPassword - adminUsername $username - adminPassword $password
107+ Enable-RDP
108+ Configure- Firewall
109+ Start-RDPService
110+
111+ Write-Output " [Coder RDP Setup] RDP configuration completed successfully!"
112+ Write-Output " [Coder RDP Setup] You can now connect using:"
113+ Write-Output " Username: $username "
114+ Write-Output " Password: [hidden]"
115+ Write-Output " Port: 3389 (default)"
116+
117+ } catch {
118+ Write-Error " [Coder RDP Setup] An unexpected error occurred: $_ "
119+ exit 1
120+ }
0 commit comments