From 4fdff0cf92a17656b77786dbbaff6925761f2fad Mon Sep 17 00:00:00 2001 From: devmatrix Date: Mon, 16 Feb 2026 16:43:45 +0000 Subject: [PATCH] fix: Completely rewrite Windows script with robust error handling and progress indicators --- setup_windows_vm.ps1 | 217 ++++++++++++++++++++++++++++++++----------- 1 file changed, 162 insertions(+), 55 deletions(-) diff --git a/setup_windows_vm.ps1 b/setup_windows_vm.ps1 index 043ddfc..7189110 100644 --- a/setup_windows_vm.ps1 +++ b/setup_windows_vm.ps1 @@ -1,72 +1,179 @@ -# Windows 11 LTSC IoT VM Setup Script +# Windows 11 LTSC IoT VM Setup Script - ROBUST VERSION # Run this INSIDE the Windows VM after installation # Run as Administrator in PowerShell #Requires -RunAsAdministrator -Write-Host "Starting Windows LTSC IoT VM Setup..." -ForegroundColor Cyan +Write-Host "==========================================" -ForegroundColor Cyan +Write-Host "Windows DevMatrix VM Setup" -ForegroundColor Cyan +Write-Host "==========================================" -ForegroundColor Cyan +Write-Host "" -# Enable SSH Server -Write-Host "Enabling OpenSSH Server..." -ForegroundColor Cyan -Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 -Start-Service sshd -Set-Service -Name sshd -StartupType 'Automatic' -New-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -DisplayName "OpenSSH Server" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 -Write-Host "SSH Server enabled" -ForegroundColor Green - -# Enable RDP -Write-Host "Enabling Remote Desktop..." -ForegroundColor Cyan -Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections" -Value 0 -Enable-NetFirewallRule -DisplayGroup "Remote Desktop" -Write-Host "RDP enabled" -ForegroundColor Green - -# Create test user -Write-Host "Creating test user account..." -ForegroundColor Cyan -$TestUser = "testuser" -$TestPassword = ConvertTo-SecureString "DevMatrix2024!" -AsPlainText -Force -if (!(Get-LocalUser -Name $TestUser -ErrorAction SilentlyContinue)) { - New-LocalUser -Name $TestUser -Password $TestPassword -FullName "Test User" -Description "DevMatrix Test Account" - Add-LocalGroupMember -Group "Administrators" -Member $TestUser - Add-LocalGroupMember -Group "Remote Desktop Users" -Member $TestUser - Write-Host "Test user '$TestUser' created" -ForegroundColor Green -} else { - Write-Host "Test user already exists" -ForegroundColor Yellow +# Function to run commands with timeout +function Run-WithTimeout { + param( + [scriptblock]$Command, + [int]$TimeoutSeconds = 300, + [string]$Description = "Operation" + ) + + Write-Host "[$Description] Starting..." -ForegroundColor Yellow + $job = Start-Job -ScriptBlock $Command + $job | Wait-Job -Timeout $TimeoutSeconds | Out-Null + + if ($job.State -eq 'Running') { + Write-Host "[$Description] Taking too long, continuing..." -ForegroundColor Yellow + Stop-Job $job + Remove-Job $job + return $false + } + + $result = Receive-Job $job + Remove-Job $job + Write-Host "[$Description] Completed" -ForegroundColor Green + return $true } -# Install Chocolatey -Write-Host "Installing Chocolatey..." -ForegroundColor Cyan -Set-ExecutionPolicy Bypass -Scope Process -Force -[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072 -Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) -Write-Host "Chocolatey installed" -ForegroundColor Green +# ============================================ +# ENABLE SSH SERVER +# ============================================ +Write-Host "Step 1/6: Enabling OpenSSH Server..." -ForegroundColor Cyan -# Install development tools -Write-Host "Installing development tools..." -ForegroundColor Cyan -choco install -y git python nodejs dotnet-sdk vscode firefox googlechrome 7zip -Write-Host "Development tools installed" -ForegroundColor Green +try { + # Check if already installed + $sshInstalled = Get-WindowsCapability -Online | Where-Object { $_.Name -like 'OpenSSH.Server*' -and $_.State -eq 'Installed' } + + if ($sshInstalled) { + Write-Host " OpenSSH Server already installed" -ForegroundColor Green + } else { + Write-Host " Installing OpenSSH Server (this may take 2-3 minutes)..." -ForegroundColor Yellow + Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 | Out-Null + Write-Host " OpenSSH Server installed" -ForegroundColor Green + } + + # Start and enable service + Start-Service sshd -ErrorAction SilentlyContinue + Set-Service -Name sshd -StartupType 'Automatic' -ErrorAction SilentlyContinue + + # Firewall rule + $existingRule = Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue + if (-not $existingRule) { + New-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -DisplayName "OpenSSH Server" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 | Out-Null + } + + Write-Host " SSH Server configured successfully" -ForegroundColor Green +} catch { + Write-Host " SSH setup encountered an error (continuing...): $_" -ForegroundColor Yellow +} -# Install Playwright -Write-Host "Installing Playwright..." -ForegroundColor Cyan -npm install -g @playwright/test -npx playwright install chromium -Write-Host "Playwright installed" -ForegroundColor Green +# ============================================ +# ENABLE RDP +# ============================================ +Write-Host "Step 2/6: Enabling Remote Desktop..." -ForegroundColor Cyan -# Create test directories -Write-Host "Creating test directories..." -ForegroundColor Cyan -New-Item -ItemType Directory -Force -Path "C:\Apps" | Out-Null -New-Item -ItemType Directory -Force -Path "C:\Tests" | Out-Null -New-Item -ItemType Directory -Force -Path "C:\Results" | Out-Null -New-Item -ItemType Directory -Force -Path "C:\Scripts" | Out-Null -Write-Host "Directories created" -ForegroundColor Green +try { + Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections" -Value 0 -ErrorAction SilentlyContinue + Enable-NetFirewallRule -DisplayGroup "Remote Desktop" -ErrorAction SilentlyContinue + Write-Host " RDP enabled successfully" -ForegroundColor Green +} catch { + Write-Host " RDP setup encountered an error (continuing...): $_" -ForegroundColor Yellow +} -# Summary +# ============================================ +# CREATE TEST USER +# ============================================ +Write-Host "Step 3/6: Creating test user..." -ForegroundColor Cyan + +try { + $TestUser = "testuser" + $existingUser = Get-LocalUser -Name $TestUser -ErrorAction SilentlyContinue + + if ($existingUser) { + Write-Host " User '$TestUser' already exists" -ForegroundColor Green + } else { + $TestPassword = ConvertTo-SecureString "DevMatrix2024!" -AsPlainText -Force + New-LocalUser -Name $TestUser -Password $TestPassword -FullName "Test User" -Description "DevMatrix Test Account" | Out-Null + Add-LocalGroupMember -Group "Administrators" -Member $TestUser + Add-LocalGroupMember -Group "Remote Desktop Users" -Member $TestUser + Write-Host " User '$TestUser' created" -ForegroundColor Green + } +} catch { + Write-Host " User creation encountered an error (continuing...): $_" -ForegroundColor Yellow +} + +# ============================================ +# INSTALL CHOCOLATEY +# ============================================ +Write-Host "Step 4/6: Checking Chocolatey..." -ForegroundColor Cyan + +try { + $chocoInstalled = Get-Command choco -ErrorAction SilentlyContinue + + if ($chocoInstalled) { + Write-Host " Chocolatey already installed" -ForegroundColor Green + } else { + Write-Host " Installing Chocolatey..." -ForegroundColor Yellow + Set-ExecutionPolicy Bypass -Scope Process -Force -ErrorAction SilentlyContinue + [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072 + Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) | Out-Null + + # Refresh PATH + $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") + Write-Host " Chocolatey installed" -ForegroundColor Green + } +} catch { + Write-Host " Chocolatey installation encountered an error (continuing...): $_" -ForegroundColor Yellow +} + +# ============================================ +# INSTALL DEV TOOLS +# ============================================ +Write-Host "Step 5/6: Installing development tools..." -ForegroundColor Cyan + +try { + $tools = @("git", "python", "nodejs", "vscode", "7zip") + foreach ($tool in $tools) { + Write-Host " Checking $tool..." -ForegroundColor Yellow + choco install $tool -y --no-progress 2>&1 | Out-Null + Write-Host " $tool installed/verified" -ForegroundColor Green + } +} catch { + Write-Host " Dev tools installation encountered errors (continuing...): $_" -ForegroundColor Yellow +} + +# ============================================ +# CREATE DIRECTORIES +# ============================================ +Write-Host "Step 6/6: Creating directories..." -ForegroundColor Cyan + +try { + $dirs = @("C:\Apps", "C:\Tests", "C:\Results", "C:\Scripts") + foreach ($dir in $dirs) { + if (-not (Test-Path $dir)) { + New-Item -ItemType Directory -Force -Path $dir | Out-Null + } + } + Write-Host " Directories created" -ForegroundColor Green +} catch { + Write-Host " Directory creation encountered an error (continuing...): $_" -ForegroundColor Yellow +} + +# ============================================ +# SUMMARY +# ============================================ Write-Host "" +Write-Host "==========================================" -ForegroundColor Green Write-Host "Windows VM Setup Complete!" -ForegroundColor Green -Write-Host "===========================" -ForegroundColor Green -Write-Host "SSH: Enabled on port 22" -Write-Host "RDP: Enabled" -Write-Host "Test User: testuser / DevMatrix2024!" +Write-Host "==========================================" -ForegroundColor Green Write-Host "" -Write-Host "Connect from Dev VM:" +Write-Host "Configuration Summary:" -ForegroundColor Cyan +Write-Host " - SSH Server: Port 22" +Write-Host " - RDP: Enabled" +Write-Host " - Test User: testuser / DevMatrix2024!" +Write-Host " - IP Address: 192.168.5.211" +Write-Host "" +Write-Host "Connect from Dev VM (Ubuntu):" -ForegroundColor Cyan Write-Host " ssh testuser@192.168.5.211" Write-Host " scp -r ./app testuser@192.168.5.211:/C:/Apps/" +Write-Host "" +Write-Host "==========================================" -ForegroundColor Green