-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.ps1
More file actions
275 lines (238 loc) · 9.72 KB
/
uninstall.ps1
File metadata and controls
275 lines (238 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# PersistenceAI Uninstaller (PowerShell)
# This script can be downloaded and executed via:
# iwr -useb https://persistence-ai.github.io/Landing/uninstall.ps1 | iex
# curl -fsSL https://persistence-ai.github.io/Landing/uninstall.ps1 | powershell -ExecutionPolicy Bypass -Command -
$ErrorActionPreference = "Continue"
# ============================================================================
# Output Functions (Matching install.ps1 style)
# ============================================================================
function Write-Info {
param([string]$msg)
Write-Host " " -NoNewline
Write-Host "[i]" -ForegroundColor Cyan -NoNewline
Write-Host " $msg" -ForegroundColor Gray
}
function Write-Success {
param([string]$msg)
Write-Host " " -NoNewline
Write-Host "[+]" -ForegroundColor Green -NoNewline
Write-Host " $msg" -ForegroundColor Gray
}
function Write-Error {
param([string]$msg)
Write-Host " " -NoNewline
Write-Host "[x]" -ForegroundColor Red -NoNewline
Write-Host " $msg" -ForegroundColor Gray
}
function Write-Warning {
param([string]$msg)
Write-Host " " -NoNewline
Write-Host "[!]" -ForegroundColor Yellow -NoNewline
Write-Host " $msg" -ForegroundColor Gray
}
function Write-Step {
param([string]$msg)
Write-Host " " -NoNewline
Write-Host "[>]" -ForegroundColor Cyan -NoNewline
Write-Host " $msg" -ForegroundColor White
}
# ============================================================================
# Banner
# ============================================================================
Write-Host ""
Write-Host " " -NoNewline; Write-Host "========================================" -ForegroundColor Magenta
Write-Host " " -NoNewline; Write-Host "|" -ForegroundColor Magenta -NoNewline
Write-Host " " -NoNewline; Write-Host "PersistenceAI" -ForegroundColor Magenta -NoNewline; Write-Host " Uninstaller" -ForegroundColor White -NoNewline; Write-Host " " -NoNewline; Write-Host "|" -ForegroundColor Magenta
Write-Host " " -NoNewline; Write-Host "========================================" -ForegroundColor Magenta
Write-Host ""
# ============================================================================
# Windows API for MoveFileEx (Delete on Reboot)
# ============================================================================
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);
public const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x4;
}
"@
function Register-ForDeletionOnReboot {
param([string]$FilePath)
try {
if (Test-Path $FilePath) {
$result = [Win32]::MoveFileEx($FilePath, $null, [Win32]::MOVEFILE_DELAY_UNTIL_REBOOT)
if ($result) {
Write-Info "Marked for deletion on reboot: $FilePath"
return $true
}
}
} catch {
# Silently fail - not critical
}
return $false
}
# ============================================================================
# Process Termination (Aggressive - handles file locks)
# ============================================================================
function Stop-PersistenceAIProcesses {
Write-Step "Terminating PersistenceAI processes..."
# Find processes by name
$processes = Get-Process | Where-Object {
($_.ProcessName -eq "pai") -or
($_.ProcessName -eq "persistenceai") -or
($_.ProcessName -like "*persistenceai*")
} -ErrorAction SilentlyContinue
if ($processes) {
Write-Info "Found $($processes.Count) process(es) to terminate"
foreach ($proc in $processes) {
try {
Write-Info "Terminating process: $($proc.ProcessName) (PID: $($proc.Id))"
# Use taskkill with /T to kill process tree (child processes)
$result = Start-Process -FilePath "taskkill" -ArgumentList "/F", "/T", "/PID", $proc.Id -Wait -NoNewWindow -PassThru -ErrorAction SilentlyContinue
if ($result -and $result.ExitCode -eq 0) {
Write-Success "Terminated process tree: $($proc.ProcessName)"
} else {
# Fallback to Stop-Process
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
}
} catch {
Write-Warning "Error terminating process: $_"
}
}
} else {
Write-Info "No PersistenceAI processes found"
}
# Wait for processes to fully terminate
Start-Sleep -Seconds 3
}
# ============================================================================
# File/Directory Removal with Retry and Reboot Marking
# ============================================================================
function Remove-ItemWithRetry {
param(
[string]$Path,
[int]$MaxRetries = 3,
[int]$DelaySeconds = 2
)
if (-not (Test-Path $Path)) {
return $true
}
for ($i = 1; $i -le $MaxRetries; $i++) {
try {
if (Test-Path $Path -PathType Container) {
Remove-Item -Path $Path -Recurse -Force -ErrorAction Stop
} else {
Remove-Item -Path $Path -Force -ErrorAction Stop
}
Write-Success "Removed: $Path"
return $true
} catch {
if ($i -lt $MaxRetries) {
Write-Warning "Attempt $i failed, retrying in $DelaySeconds seconds..."
Start-Sleep -Seconds $DelaySeconds
} else {
Write-Warning "Failed to remove after $MaxRetries attempts: $Path"
Write-Info "Marking for deletion on reboot..."
if (Test-Path $Path -PathType Container) {
# For directories, mark all files inside
Get-ChildItem -Path $Path -Recurse -File -ErrorAction SilentlyContinue | ForEach-Object {
Register-ForDeletionOnReboot $_.FullName
}
Register-ForDeletionOnReboot $Path
} else {
Register-ForDeletionOnReboot $Path
}
return $false
}
}
}
return $false
}
# ============================================================================
# Main Uninstall Logic
# ============================================================================
Write-Step "Starting uninstall of PersistenceAI..."
# Step 1: Stop all processes (aggressive termination)
Stop-PersistenceAIProcesses
# Step 2: Find all installation directories
Write-Step "Locating installation directories..."
$dirsToRemove = @(
"$env:USERPROFILE\.persistenceai",
"$env:USERPROFILE\.pai",
"$env:USERPROFILE\.config\persistenceai",
"$env:USERPROFILE\.config\pai",
"$env:LOCALAPPDATA\persistenceai",
"$env:LOCALAPPDATA\pai"
)
# Also find installation directories from PATH
$paiCmd = Get-Command -Name "pai" -ErrorAction SilentlyContinue
$persistenceaiCmd = Get-Command -Name "persistenceai" -ErrorAction SilentlyContinue
if ($paiCmd) {
$binDir = Split-Path $paiCmd.Source -Parent
$installDir = Split-Path $binDir -Parent
if ($installDir -and $installDir -notin $dirsToRemove) {
$dirsToRemove += $installDir
}
# Also add the bin directory itself
if ($binDir -and $binDir -notin $dirsToRemove) {
$dirsToRemove += $binDir
}
}
if ($persistenceaiCmd) {
$binDir = Split-Path $persistenceaiCmd.Source -Parent
$installDir = Split-Path $binDir -Parent
if ($installDir -and $installDir -notin $dirsToRemove) {
$dirsToRemove += $installDir
}
# Also add the bin directory itself
if ($binDir -and $binDir -notin $dirsToRemove) {
$dirsToRemove += $binDir
}
}
# Remove duplicates
$dirsToRemove = $dirsToRemove | Select-Object -Unique
# Step 3: Remove directories with retry logic
Write-Step "Removing installation directories..."
$removedCount = 0
$markedForReboot = 0
foreach ($dir in $dirsToRemove) {
if (Test-Path $dir) {
Write-Info "Removing: $dir"
if (Remove-ItemWithRetry -Path $dir) {
$removedCount++
} else {
$markedForReboot++
}
}
}
# Step 4: Clean PATH
Write-Step "Cleaning PATH environment variable..."
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath) {
$newUserPath = ($userPath -split ";" | Where-Object {
$_ -and $_ -notmatch "\.persistenceai" -and $_ -notmatch "\.pai"
}) -join ";"
[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")
Write-Success "Cleaned user PATH"
}
# Update current session PATH
$env:Path = ($env:Path -split ";" | Where-Object {
$_ -and $_ -notmatch "\.persistenceai" -and $_ -notmatch "\.pai"
}) -join ";"
# Step 5: Summary
Write-Host ""
Write-Host " " -NoNewline; Write-Host "================================" -ForegroundColor DarkGray
if ($markedForReboot -gt 0) {
Write-Warning "Some files could not be removed and were marked for deletion on reboot"
Write-Info "Please restart your computer to complete the uninstallation"
Write-Info "Removed $removedCount directory/directories"
Write-Info "$markedForReboot item(s) marked for deletion on reboot"
} else {
Write-Success "PersistenceAI uninstalled successfully"
Write-Info "Removed $removedCount directory/directories"
}
Write-Host " " -NoNewline; Write-Host "================================" -ForegroundColor DarkGray
Write-Host ""
Write-Host " " -NoNewline; Write-Host "Note:" -ForegroundColor Yellow -NoNewline; Write-Host " Restart PowerShell for PATH changes to take effect" -ForegroundColor Gray
Write-Host ""