fix: Completely rewrite Windows script with robust error handling and progress indicators
This commit is contained in:
parent
afedda58d7
commit
4fdff0cf92
|
|
@ -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
|
|
||||||
|
|
||||||
# Create test user
|
if ($job.State -eq 'Running') {
|
||||||
Write-Host "Creating test user account..." -ForegroundColor Cyan
|
Write-Host "[$Description] Taking too long, continuing..." -ForegroundColor Yellow
|
||||||
$TestUser = "testuser"
|
Stop-Job $job
|
||||||
$TestPassword = ConvertTo-SecureString "DevMatrix2024!" -AsPlainText -Force
|
Remove-Job $job
|
||||||
if (!(Get-LocalUser -Name $TestUser -ErrorAction SilentlyContinue)) {
|
return $false
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Install Chocolatey
|
$result = Receive-Job $job
|
||||||
Write-Host "Installing Chocolatey..." -ForegroundColor Cyan
|
Remove-Job $job
|
||||||
Set-ExecutionPolicy Bypass -Scope Process -Force
|
Write-Host "[$Description] Completed" -ForegroundColor Green
|
||||||
|
return $true
|
||||||
|
}
|
||||||
|
|
||||||
|
# ============================================
|
||||||
|
# ENABLE SSH SERVER
|
||||||
|
# ============================================
|
||||||
|
Write-Host "Step 1/6: Enabling OpenSSH Server..." -ForegroundColor Cyan
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
# ============================================
|
||||||
|
# ENABLE RDP
|
||||||
|
# ============================================
|
||||||
|
Write-Host "Step 2/6: Enabling Remote Desktop..." -ForegroundColor Cyan
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
# ============================================
|
||||||
|
# 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
|
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
|
||||||
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
|
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
|
Write-Host " Chocolatey installed" -ForegroundColor Green
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
Write-Host " Chocolatey installation encountered an error (continuing...): $_" -ForegroundColor Yellow
|
||||||
|
}
|
||||||
|
|
||||||
# Install development tools
|
# ============================================
|
||||||
Write-Host "Installing development tools..." -ForegroundColor Cyan
|
# INSTALL DEV TOOLS
|
||||||
choco install -y git python nodejs dotnet-sdk vscode firefox googlechrome 7zip
|
# ============================================
|
||||||
Write-Host "Development tools installed" -ForegroundColor Green
|
Write-Host "Step 5/6: Installing development tools..." -ForegroundColor Cyan
|
||||||
|
|
||||||
# Install Playwright
|
try {
|
||||||
Write-Host "Installing Playwright..." -ForegroundColor Cyan
|
$tools = @("git", "python", "nodejs", "vscode", "7zip")
|
||||||
npm install -g @playwright/test
|
foreach ($tool in $tools) {
|
||||||
npx playwright install chromium
|
Write-Host " Checking $tool..." -ForegroundColor Yellow
|
||||||
Write-Host "Playwright installed" -ForegroundColor Green
|
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 test directories
|
# ============================================
|
||||||
Write-Host "Creating test directories..." -ForegroundColor Cyan
|
# CREATE DIRECTORIES
|
||||||
New-Item -ItemType Directory -Force -Path "C:\Apps" | Out-Null
|
# ============================================
|
||||||
New-Item -ItemType Directory -Force -Path "C:\Tests" | Out-Null
|
Write-Host "Step 6/6: Creating directories..." -ForegroundColor Cyan
|
||||||
New-Item -ItemType Directory -Force -Path "C:\Results" | Out-Null
|
|
||||||
New-Item -ItemType Directory -Force -Path "C:\Scripts" | Out-Null
|
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
|
Write-Host " Directories created" -ForegroundColor Green
|
||||||
|
} catch {
|
||||||
|
Write-Host " Directory creation encountered an error (continuing...): $_" -ForegroundColor Yellow
|
||||||
|
}
|
||||||
|
|
||||||
# Summary
|
# ============================================
|
||||||
|
# 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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue