first commit
This commit is contained in:
46
清除Windows日志.ps1
Executable file
46
清除Windows日志.ps1
Executable file
@@ -0,0 +1,46 @@
|
||||
# Clear Windows Event Logs - Run as Administrator
|
||||
# Clears Application, System, Security, Setup, and other logs
|
||||
|
||||
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
||||
Write-Host "Need Administrator. Right-click -> Run as administrator." -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
$mainLogs = @('Application', 'Security', 'System', 'Setup')
|
||||
$cleared = @()
|
||||
$failed = @()
|
||||
|
||||
Write-Host "Clearing Windows Event Logs..." -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
foreach ($log in $mainLogs) {
|
||||
try {
|
||||
Clear-EventLog -LogName $log -ErrorAction Stop
|
||||
$cleared += $log
|
||||
Write-Host " OK: $log" -ForegroundColor Green
|
||||
} catch {
|
||||
$failed += "$log : $($_.Exception.Message)"
|
||||
Write-Host " FAIL: $log - $($_.Exception.Message)" -ForegroundColor Yellow
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Clearing other logs via wevtutil..." -ForegroundColor Cyan
|
||||
$allLogs = wevtutil el 2>$null
|
||||
foreach ($log in $allLogs) {
|
||||
if ($mainLogs -notcontains $log) {
|
||||
$r = wevtutil cl $log 2>&1
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
$cleared += $log
|
||||
Write-Host " OK: $log" -ForegroundColor Green
|
||||
} else {
|
||||
$failed += "$log : $r"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Done. Cleared: $($cleared.Count) logs." -ForegroundColor Green
|
||||
if ($failed.Count -gt 0) {
|
||||
Write-Host "Failed: $($failed.Count) logs (some may be protected)." -ForegroundColor Yellow
|
||||
}
|
||||
Reference in New Issue
Block a user