# ============================================================================== # Install-TestHarness.ps1 - Registers Local Test Agent & Boot Recovery on Client PC # Run once as Administrator on Client PC B. # ============================================================================== # Require Administrator $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Error "Please run this script as Administrator (以管理员身份运行)!" exit 1 } $root = $PSScriptRoot if (-not $root) { $root = Get-Location } $harnessDir = "C:\ProgramData\ClassNotify-TestHarness" $subDirs = @("agent", "sessions", "reports", "state", "logs", "modules") Write-Host "==========================================================" -ForegroundColor Cyan Write-Host " Installing ClassNotify Automated Test Harness on PC B " -ForegroundColor Cyan Write-Host "==========================================================" -ForegroundColor Cyan foreach ($sub in $subDirs) { $targetPath = Join-Path $harnessDir $sub if (-not (Test-Path $targetPath)) { New-Item -ItemType Directory -Path $targetPath -Force | Out-Null } } # Copy scripts and modules to ProgramData $modulesSource = Join-Path $root "modules" if (Test-Path $modulesSource) { Copy-Item -Path "$modulesSource\*" -Destination (Join-Path $harnessDir "modules") -Recurse -Force } $agentSource = Join-Path $root "ChaosAgent.ps1" if (Test-Path $agentSource) { Copy-Item -Path $agentSource -Destination (Join-Path $harnessDir "agent\ChaosAgent.ps1") -Force } # Safe Startup Invariant: Register Windows Scheduled Task for startup boot-recovery # The task starts with immediate stale firewall cleanup and session recovery $taskName = "ClassNotify-TestHarness" $agentPath = "C:\ProgramData\ClassNotify-TestHarness\agent\ChaosAgent.ps1" $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$agentPath`" -Daemon" $trigger = New-ScheduledTaskTrigger -AtStartup $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Hours 24) # Unregister old task if exists Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Description "ClassNotify Automated Chaos Test Harness" | Out-Null Write-Host "`nInstallation Complete! You can now launch test sessions unattended." -ForegroundColor Cyan