Files
AutopilotMonitor/scripts/Bootstrap/Uninstall-AutopilotMonitor.ps1
T
Oliver Kieselbach 3a898b5071 Bootstrap leaves nothing behind: loader 1.3 and installer 2.7 (D-375)
- Loader 1.3 downloads into a protected run folder of its own
  (%ProgramData%\AutopilotMonitor-Loader-<8 hex>), runs the installer only when it
  is signed by the pinned publisher and declares the installer role and version
  2.7 or later, removes the folder after a successful run and never touches the
  agent folder. Unbound arguments now reach the installer by name; the array
  splat bound them by position.
- Installer 2.7 refuses without elevation and decides before it writes: guards
  read HKLM through the 64-bit view (a 32-bit host never saw the deployment
  marker), guard 5 is covered by the agent check. An agent's folder stays as it
  is, a skip without agent removes the folder, an install starts from an emptied,
  protected folder and stages the ZIP in a run folder. It moves the MSI's working
  directory out, writes stderr only on failure and removes the copy older loaders
  left at the fixed path.
- Help texts of both scripts state what customers need; .CHANGELOG, which made
  Get-Help ignore the help, gives way to the Platform Changelog.
- OOBE go-script: the installer's shared helpers, its own elevation check
  (#Requires is ignored under irm | iex), a run folder.
- Publish: version read line-anchored, the loader's own check on the exact
  publish bytes, installers before loaders, -Channel dev (dispatch input).
- Bootstrap MSI 1.2.0; Test-ShouldBootstrapAgent reads the 64-bit view;
  Uninstall removes loader run folders and keeps the Deployed marker.
- Tests: Pester unit suites, an integration harness running the real scripts
  (loader 1.2 and the MSI 1.0 runner frozen in TestData), BootstrapContractTests;
  the gate runs on windows-2022 and windows-2025, once more as SYSTEM, and against
  a real standard user.

[skip actions]: published by a dispatch of publish-scripts.yml, channel dev first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 13:34:54 +02:00

87 lines
3.1 KiB
PowerShell

<#
.SYNOPSIS
Cleanup script to manually remove the Autopilot Monitor agent.
.DESCRIPTION
This script removes all traces of the Autopilot Monitor agent:
- Stops and removes the Scheduled Task
- Deletes C:\ProgramData\AutopilotMonitor folder
- Deletes C:\ProgramData\AutopilotMonitor-Loader-* folders that a failed loader run kept
Normally the agent self-destructs after enrollment completion.
Use this script only for manual cleanup or troubleshooting.
.EXAMPLE
.\Uninstall-AutopilotMonitor.ps1
.NOTES
- Requires Administrator privileges
- Safe to run multiple times
- Keeps HKLM\SOFTWARE\AutopilotMonitor\Deployed: the bootstrap reads it and never
installs the agent on this device again
#>
[CmdletBinding()]
param()
$TaskName = "AutopilotMonitor-Agent"
$AgentBasePath = "$env:ProgramData\AutopilotMonitor"
Write-Host "===== Autopilot Monitor Cleanup Started =====" -ForegroundColor Cyan
# Stop and remove Scheduled Task
try {
$task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
if ($null -ne $task) {
Write-Host "Stopping Scheduled Task..." -ForegroundColor Yellow
Stop-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
Write-Host "Removing Scheduled Task..." -ForegroundColor Yellow
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction Stop
Write-Host " [OK] Scheduled Task removed" -ForegroundColor Green
}
else {
Write-Host " [SKIP] Scheduled Task not found (already removed)" -ForegroundColor Gray
}
}
catch {
Write-Host " [FAIL] Error removing Scheduled Task: $($_.Exception.Message)" -ForegroundColor Red
}
# Delete agent folder
try {
if (Test-Path $AgentBasePath) {
Write-Host "Deleting agent folder: $AgentBasePath" -ForegroundColor Yellow
# Wait a moment to ensure agent has stopped
Start-Sleep -Seconds 2
Remove-Item -Path $AgentBasePath -Recurse -Force -ErrorAction Stop
Write-Host " [OK] Agent folder deleted" -ForegroundColor Green
}
else {
Write-Host " [SKIP] Agent folder not found (already removed)" -ForegroundColor Gray
}
}
catch {
Write-Host " [FAIL] Error deleting folder: $($_.Exception.Message)" -ForegroundColor Red
Write-Host " [INFO] You may need to manually delete: $AgentBasePath" -ForegroundColor Yellow
}
# Delete loader run folders kept by failed runs
foreach ($runFolder in @(Get-ChildItem -Path $env:ProgramData -Directory -Filter 'AutopilotMonitor-Loader-*' -ErrorAction SilentlyContinue)) {
try {
Remove-Item -LiteralPath $runFolder.FullName -Recurse -Force -ErrorAction Stop
Write-Host " [OK] Loader run folder deleted: $($runFolder.FullName)" -ForegroundColor Green
}
catch {
Write-Host " [FAIL] Error deleting $($runFolder.FullName): $($_.Exception.Message)" -ForegroundColor Red
}
}
Write-Host ""
Write-Host "===== Cleanup Completed =====" -ForegroundColor Cyan
Write-Host "Autopilot Monitor agent has been removed from this device." -ForegroundColor Green
Write-Host ""