35 lines
1.1 KiB
PowerShell
35 lines
1.1 KiB
PowerShell
# Force remove stale agent releases (not current)
|
|
$ErrorActionPreference = 'Continue'
|
|
$keepName = 'agent-20260913-210940'
|
|
Get-ChildItem 'E:\wwwroot\agent\releases' | Where-Object { $_.PSIsContainer } | ForEach-Object {
|
|
$name = $_.Name
|
|
$full = $_.FullName
|
|
if ($name -eq $keepName) {
|
|
Write-Output ('KEEP ' + $name)
|
|
return
|
|
}
|
|
Write-Output ('CLEAN ' + $name)
|
|
Get-CimInstance Win32_Process | ForEach-Object {
|
|
$cl = $_.CommandLine
|
|
if ($cl -and $cl.Contains($full)) {
|
|
Write-Output ('kill pid=' + $_.ProcessId)
|
|
Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue
|
|
}
|
|
}
|
|
Start-Sleep -Seconds 1
|
|
cmd /c ("rmdir /s /q `"$full`"") | Out-Null
|
|
if (Test-Path $full) {
|
|
try {
|
|
Remove-Item $full -Recurse -Force -ErrorAction Stop
|
|
Write-Output ('REMOVED_PS ' + $name)
|
|
} catch {
|
|
Write-Output ('FAIL ' + $name + ' ' + $_.Exception.Message)
|
|
}
|
|
} else {
|
|
Write-Output ('REMOVED ' + $name)
|
|
}
|
|
}
|
|
Write-Output '=== left ==='
|
|
Get-ChildItem 'E:\wwwroot\agent\releases' | ForEach-Object { $_.Name }
|
|
Write-Output 'FORCE_CLEAN_OK'
|