fix: Completely rewrite Windows script with robust error handling and progress indicators

This commit is contained in:
devmatrix 2026-02-16 16:43:45 +00:00
parent afedda58d7
commit 4fdff0cf92
1 changed files with 162 additions and 55 deletions

View File

@ -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 this INSIDE the Windows VM after installation
# Run as Administrator in PowerShell # Run as Administrator in PowerShell
#Requires -RunAsAdministrator #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 # Function to run commands with timeout
Write-Host "Enabling OpenSSH Server..." -ForegroundColor Cyan function Run-WithTimeout {
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 param(
Start-Service sshd [scriptblock]$Command,
Set-Service -Name sshd -StartupType 'Automatic' [int]$TimeoutSeconds = 300,
New-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -DisplayName "OpenSSH Server" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 [string]$Description = "Operation"
Write-Host "SSH Server enabled" -ForegroundColor Green )
# Enable RDP Write-Host "[$Description] Starting..." -ForegroundColor Yellow
Write-Host "Enabling Remote Desktop..." -ForegroundColor Cyan $job = Start-Job -ScriptBlock $Command
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections" -Value 0 $job | Wait-Job -Timeout $TimeoutSeconds | Out-Null
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
Write-Host "RDP enabled" -ForegroundColor Green if ($job.State -eq 'Running') {
Write-Host "[$Description] Taking too long, continuing..." -ForegroundColor Yellow
# Create test user Stop-Job $job
Write-Host "Creating test user account..." -ForegroundColor Cyan Remove-Job $job
$TestUser = "testuser" return $false
$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" $result = Receive-Job $job
Add-LocalGroupMember -Group "Administrators" -Member $TestUser Remove-Job $job
Add-LocalGroupMember -Group "Remote Desktop Users" -Member $TestUser Write-Host "[$Description] Completed" -ForegroundColor Green
Write-Host "Test user '$TestUser' created" -ForegroundColor Green return $true
} else {
Write-Host "Test user already exists" -ForegroundColor Yellow
} }
# Install Chocolatey # ============================================
Write-Host "Installing Chocolatey..." -ForegroundColor Cyan # ENABLE SSH SERVER
Set-ExecutionPolicy Bypass -Scope Process -Force # ============================================
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072 Write-Host "Step 1/6: Enabling OpenSSH Server..." -ForegroundColor Cyan
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Write-Host "Chocolatey installed" -ForegroundColor Green
# Install development tools try {
Write-Host "Installing development tools..." -ForegroundColor Cyan # Check if already installed
choco install -y git python nodejs dotnet-sdk vscode firefox googlechrome 7zip $sshInstalled = Get-WindowsCapability -Online | Where-Object { $_.Name -like 'OpenSSH.Server*' -and $_.State -eq 'Installed' }
Write-Host "Development tools installed" -ForegroundColor Green
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 # ENABLE RDP
npm install -g @playwright/test # ============================================
npx playwright install chromium Write-Host "Step 2/6: Enabling Remote Desktop..." -ForegroundColor Cyan
Write-Host "Playwright installed" -ForegroundColor Green
# Create test directories try {
Write-Host "Creating test directories..." -ForegroundColor Cyan Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections" -Value 0 -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Force -Path "C:\Apps" | Out-Null Enable-NetFirewallRule -DisplayGroup "Remote Desktop" -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Force -Path "C:\Tests" | Out-Null Write-Host " RDP enabled successfully" -ForegroundColor Green
New-Item -ItemType Directory -Force -Path "C:\Results" | Out-Null } catch {
New-Item -ItemType Directory -Force -Path "C:\Scripts" | Out-Null Write-Host " RDP setup encountered an error (continuing...): $_" -ForegroundColor Yellow
Write-Host "Directories created" -ForegroundColor Green }
# 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 ""
Write-Host "==========================================" -ForegroundColor Green
Write-Host "Windows VM Setup Complete!" -ForegroundColor Green Write-Host "Windows VM Setup Complete!" -ForegroundColor Green
Write-Host "===========================" -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 "" 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 " ssh testuser@192.168.5.211"
Write-Host " scp -r ./app testuser@192.168.5.211:/C:/Apps/" Write-Host " scp -r ./app testuser@192.168.5.211:/C:/Apps/"
Write-Host ""
Write-Host "==========================================" -ForegroundColor Green