|
| 1 | +# Terraform variables |
| 2 | +$adminPassword = "${admin_password}" |
| 3 | +$port = "${port}" |
| 4 | +$webURLPath = "${web_url_path}" |
| 5 | + |
| 6 | +function Set-LocalAdminUser { |
| 7 | + Write-Output "[INFO] Starting Set-LocalAdminUser function" |
| 8 | + $securePassword = ConvertTo-SecureString $adminPassword -AsPlainText -Force |
| 9 | + Write-Output "[DEBUG] Secure password created" |
| 10 | + Get-LocalUser -Name Administrator | Set-LocalUser -Password $securePassword |
| 11 | + Write-Output "[INFO] Administrator password set" |
| 12 | + Get-LocalUser -Name Administrator | Enable-LocalUser |
| 13 | + Write-Output "[INFO] User Administrator enabled successfully" |
| 14 | + Read-Host "[DEBUG] Press Enter to proceed to the next step" |
| 15 | +} |
| 16 | + |
| 17 | +function Get-VirtualDisplayDriverRequired { |
| 18 | + Write-Output "[INFO] Starting Get-VirtualDisplayDriverRequired function" |
| 19 | + $token = Invoke-RestMethod -Headers @{'X-aws-ec2-metadata-token-ttl-seconds' = '21600'} -Method PUT -Uri http://169.254.169.254/latest/api/token |
| 20 | + Write-Output "[DEBUG] Token acquired: $token" |
| 21 | + $instanceType = Invoke-RestMethod -Headers @{'X-aws-ec2-metadata-token' = $token} -Method GET -Uri http://169.254.169.254/latest/meta-data/instance-type |
| 22 | + Write-Output "[DEBUG] Instance type: $instanceType" |
| 23 | + $OSVersion = ((Get-ItemProperty -Path "Microsoft.PowerShell.Core\Registry::\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName) -replace "[^0-9]", '' |
| 24 | + Write-Output "[DEBUG] OS version: $OSVersion" |
| 25 | + |
| 26 | + # Force boolean result |
| 27 | + $result = (($OSVersion -ne "2019") -and ($OSVersion -ne "2022") -and ($OSVersion -ne "2025")) -and (($instanceType[0] -ne 'g') -and ($instanceType[0] -ne 'p')) |
| 28 | + Write-Output "[INFO] VirtualDisplayDriverRequired result: $result" |
| 29 | + Read-Host "[DEBUG] Press Enter to proceed to the next step" |
| 30 | + return [bool]$result |
| 31 | +} |
| 32 | + |
| 33 | +function Download-DCV { |
| 34 | + param ( |
| 35 | + [bool]$VirtualDisplayDriverRequired |
| 36 | + ) |
| 37 | + Write-Output "[INFO] Starting Download-DCV function" |
| 38 | + |
| 39 | + $downloads = @( |
| 40 | + @{ |
| 41 | + Name = "DCV Display Driver" |
| 42 | + Required = $VirtualDisplayDriverRequired |
| 43 | + Path = "C:\Windows\Temp\DCVDisplayDriver.msi" |
| 44 | + Uri = "https://d1uj6qtbmh3dt5.cloudfront.net/nice-dcv-virtual-display-x64-Release.msi" |
| 45 | + }, |
| 46 | + @{ |
| 47 | + Name = "DCV Server" |
| 48 | + Required = $true |
| 49 | + Path = "C:\Windows\Temp\DCVServer.msi" |
| 50 | + Uri = "https://d1uj6qtbmh3dt5.cloudfront.net/nice-dcv-server-x64-Release.msi" |
| 51 | + } |
| 52 | + ) |
| 53 | + |
| 54 | + foreach ($download in $downloads) { |
| 55 | + if ($download.Required -and -not (Test-Path $download.Path)) { |
| 56 | + try { |
| 57 | + Write-Output "[INFO] Downloading $($download.Name)" |
| 58 | + |
| 59 | + # Display progress manually (no events) |
| 60 | + $progressActivity = "Downloading $($download.Name)" |
| 61 | + $progressStatus = "Starting download..." |
| 62 | + Write-Progress -Activity $progressActivity -Status $progressStatus -PercentComplete 0 |
| 63 | + |
| 64 | + # Synchronously download the file |
| 65 | + $webClient = New-Object System.Net.WebClient |
| 66 | + $webClient.DownloadFile($download.Uri, $download.Path) |
| 67 | + |
| 68 | + # Update progress |
| 69 | + Write-Progress -Activity $progressActivity -Status "Completed" -PercentComplete 100 |
| 70 | + |
| 71 | + Write-Output "[INFO] $($download.Name) downloaded successfully." |
| 72 | + } catch { |
| 73 | + Write-Output "[ERROR] Failed to download $($download.Name): $_" |
| 74 | + throw |
| 75 | + } |
| 76 | + } else { |
| 77 | + Write-Output "[INFO] $($download.Name) already exists. Skipping download." |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + Write-Output "[INFO] All downloads completed" |
| 82 | + Read-Host "[DEBUG] Press Enter to proceed to the next step" |
| 83 | +} |
| 84 | + |
| 85 | +function Install-DCV { |
| 86 | + param ( |
| 87 | + [bool]$VirtualDisplayDriverRequired |
| 88 | + ) |
| 89 | + Write-Output "[INFO] Starting Install-DCV function" |
| 90 | + |
| 91 | + if (-not (Get-Service -Name "dcvserver" -ErrorAction SilentlyContinue)) { |
| 92 | + if ($VirtualDisplayDriverRequired) { |
| 93 | + Write-Output "[INFO] Installing DCV Display Driver" |
| 94 | + Start-Process "C:\Windows\System32\msiexec.exe" -ArgumentList "/I C:\Windows\Temp\DCVDisplayDriver.msi /quiet /norestart" -Wait |
| 95 | + } else { |
| 96 | + Write-Output "[INFO] DCV Display Driver installation skipped (not required)." |
| 97 | + } |
| 98 | + Write-Output "[INFO] Installing DCV Server" |
| 99 | + Start-Process "C:\Windows\System32\msiexec.exe" -ArgumentList "/I C:\Windows\Temp\DCVServer.msi ADDLOCAL=ALL /quiet /norestart /l*v C:\Windows\Temp\dcv_install_msi.log" -Wait |
| 100 | + } else { |
| 101 | + Write-Output "[INFO] DCV Server already installed, skipping installation." |
| 102 | + } |
| 103 | + |
| 104 | + # Wait for the service to appear with a timeout |
| 105 | + $timeout = 10 # seconds |
| 106 | + $elapsed = 0 |
| 107 | + while (-not (Get-Service -Name "dcvserver" -ErrorAction SilentlyContinue) -and ($elapsed -lt $timeout)) { |
| 108 | + Start-Sleep -Seconds 1 |
| 109 | + $elapsed++ |
| 110 | + } |
| 111 | + |
| 112 | + if ($elapsed -ge $timeout) { |
| 113 | + Write-Output "[WARNING] Timeout waiting for dcvserver service. A restart is required to complete installation." |
| 114 | + Restart-SystemForDCV |
| 115 | + } else { |
| 116 | + Write-Output "[INFO] dcvserver service detected successfully." |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +function Restart-SystemForDCV { |
| 121 | + Write-Output "[INFO] The system will restart in 10 seconds to finalize DCV installation." |
| 122 | + Start-Sleep -Seconds 10 |
| 123 | + |
| 124 | + # Initiate restart |
| 125 | + Restart-Computer -Force |
| 126 | + |
| 127 | + # Exit the script after initiating restart |
| 128 | + Write-Output "[INFO] Please wait for the system to restart..." |
| 129 | + |
| 130 | + Exit 1 |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +function Configure-DCV { |
| 135 | + Write-Output "[INFO] Starting Configure-DCV function" |
| 136 | + $dcvPath = "Microsoft.PowerShell.Core\Registry::\HKEY_USERS\S-1-5-18\Software\GSettings\com\nicesoftware\dcv" |
| 137 | + |
| 138 | + # Create the required paths |
| 139 | + @("$dcvPath\connectivity", "$dcvPath\session-management", "$dcvPath\session-management\automatic-console-session", "$dcvPath\display") | ForEach-Object { |
| 140 | + if (-not (Test-Path $_)) { |
| 141 | + New-Item -Path $_ -Force | Out-Null |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + # Set registry keys |
| 146 | + New-ItemProperty -Path "$dcvPath\session-management" -Name create-session -PropertyType DWORD -Value 1 -Force |
| 147 | + New-ItemProperty -Path "$dcvPath\session-management\automatic-console-session" -Name owner -Value Administrator -Force |
| 148 | + New-ItemProperty -Path "$dcvPath\connectivity" -Name quic-port -PropertyType DWORD -Value $port -Force |
| 149 | + New-ItemProperty -Path "$dcvPath\connectivity" -Name web-port -PropertyType DWORD -Value $port -Force |
| 150 | + New-ItemProperty -Path "$dcvPath\connectivity" -Name web-url-path -PropertyType String -Value $webURLPath -Force |
| 151 | + |
| 152 | + # Attempt to restart service |
| 153 | + if (Get-Service -Name "dcvserver" -ErrorAction SilentlyContinue) { |
| 154 | + Restart-Service -Name "dcvserver" |
| 155 | + } else { |
| 156 | + Write-Output "[WARNING] dcvserver service not found. Ensure the system was restarted properly." |
| 157 | + } |
| 158 | + |
| 159 | + Write-Output "[INFO] DCV configuration completed" |
| 160 | + Read-Host "[DEBUG] Press Enter to proceed to the next step" |
| 161 | +} |
| 162 | + |
| 163 | +# Main Script Execution |
| 164 | +Write-Output "[INFO] Starting script" |
| 165 | +$VirtualDisplayDriverRequired = [bool](Get-VirtualDisplayDriverRequired) |
| 166 | +Set-LocalAdminUser |
| 167 | +Download-DCV -VirtualDisplayDriverRequired $VirtualDisplayDriverRequired |
| 168 | +Install-DCV -VirtualDisplayDriverRequired $VirtualDisplayDriverRequired |
| 169 | +Configure-DCV |
| 170 | +Write-Output "[INFO] Script completed" |
0 commit comments