180 lines
7.2 KiB
PowerShell
180 lines
7.2 KiB
PowerShell
# 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 "==========================================" -ForegroundColor Cyan
|
|
Write-Host "Windows DevMatrix VM Setup" -ForegroundColor Cyan
|
|
Write-Host "==========================================" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
|
|
# 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
|
|
}
|
|
|
|
# ============================================
|
|
# 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
|
|
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 ""
|
|
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
|