mirror of
https://github.com/tw93/Mole.git
synced 2026-02-10 03:14:17 +00:00
refactor: merge repair command into optimize
Per tw93's feedback on PR #305, consolidate repair utilities into the optimize command to keep the CLI simple and unified. Changes: - Add repair flags to optimize: -Repair, -Font, -Icon, -Search, -Store - Remove standalone repair.ps1 command - Update mole.ps1 menu and help to reflect changes - optimize now handles both optimization and repair tasks Usage: mole optimize # Standard optimizations mole optimize -Repair # Optimizations + all repairs mole optimize -Icon -Font # Optimizations + specific repairs
This commit is contained in:
302
bin/optimize.ps1
302
bin/optimize.ps1
@@ -1,12 +1,18 @@
|
||||
# Mole - Optimize Command
|
||||
# System optimization and health checks for Windows
|
||||
# System optimization, health checks, and repairs for Windows
|
||||
|
||||
#Requires -Version 5.1
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[switch]$DryRun,
|
||||
[switch]$DebugMode,
|
||||
[switch]$ShowHelp
|
||||
[switch]$ShowHelp,
|
||||
# Repair options
|
||||
[switch]$Repair,
|
||||
[switch]$Font,
|
||||
[switch]$Icon,
|
||||
[switch]$Search,
|
||||
[switch]$Store
|
||||
)
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
@@ -27,6 +33,7 @@ $libDir = Join-Path (Split-Path -Parent $scriptDir) "lib"
|
||||
# ============================================================================
|
||||
|
||||
$script:OptimizationsApplied = 0
|
||||
$script:RepairsApplied = 0
|
||||
$script:IssuesFound = 0
|
||||
$script:IssuesFixed = 0
|
||||
|
||||
@@ -37,22 +44,36 @@ $script:IssuesFixed = 0
|
||||
function Show-OptimizeHelp {
|
||||
$esc = [char]27
|
||||
Write-Host ""
|
||||
Write-Host "$esc[1;35mMole Optimize$esc[0m - System optimization and health checks"
|
||||
Write-Host "$esc[1;35mMole Optimize$esc[0m - System optimization, health checks, and repairs"
|
||||
Write-Host ""
|
||||
Write-Host "$esc[33mUsage:$esc[0m mole optimize [options]"
|
||||
Write-Host ""
|
||||
Write-Host "$esc[33mOptions:$esc[0m"
|
||||
Write-Host " -DryRun Preview optimizations without applying"
|
||||
Write-Host " -DryRun Preview changes without applying"
|
||||
Write-Host " -DebugMode Enable debug logging"
|
||||
Write-Host " -ShowHelp Show this help message"
|
||||
Write-Host ""
|
||||
Write-Host "$esc[33mOptimizations:$esc[0m"
|
||||
Write-Host "$esc[33mRepair Options:$esc[0m"
|
||||
Write-Host " -Repair Run all repairs (Font, Icon, Search, Store)"
|
||||
Write-Host " -Font Rebuild font cache (fixes font display issues)"
|
||||
Write-Host " -Icon Rebuild icon cache (fixes missing/corrupt icons)"
|
||||
Write-Host " -Search Reset Windows Search index (fixes search issues)"
|
||||
Write-Host " -Store Reset Windows Store cache (fixes Store issues)"
|
||||
Write-Host ""
|
||||
Write-Host "$esc[33mExamples:$esc[0m"
|
||||
Write-Host " mole optimize # Run standard optimizations"
|
||||
Write-Host " mole optimize -Repair # Run optimizations + all repairs"
|
||||
Write-Host " mole optimize -Icon -Font # Run optimizations + specific repairs"
|
||||
Write-Host " mole optimize -DryRun # Preview what would happen"
|
||||
Write-Host ""
|
||||
Write-Host "$esc[33mOptimizations (always run):$esc[0m"
|
||||
Write-Host " - Disk defragmentation/TRIM (SSD optimization)"
|
||||
Write-Host " - Windows Search index optimization"
|
||||
Write-Host " - Windows Search index check"
|
||||
Write-Host " - DNS cache flush"
|
||||
Write-Host " - Network optimization"
|
||||
Write-Host " - Startup program analysis"
|
||||
Write-Host " - System file verification"
|
||||
Write-Host " - Disk health check"
|
||||
Write-Host " - Windows Update status"
|
||||
Write-Host ""
|
||||
}
|
||||
|
||||
@@ -440,6 +461,234 @@ function Test-WindowsUpdate {
|
||||
}
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Repair Functions
|
||||
# ============================================================================
|
||||
|
||||
function Repair-FontCache {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Rebuild Windows font cache
|
||||
.DESCRIPTION
|
||||
Stops the font cache service, clears the cache files, and restarts.
|
||||
Fixes issues with fonts not displaying correctly or missing fonts.
|
||||
#>
|
||||
|
||||
$esc = [char]27
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "$esc[34m$($script:Icons.Arrow) Font Cache Rebuild$esc[0m"
|
||||
|
||||
if (-not (Test-IsAdmin)) {
|
||||
Write-Host " $esc[33m$($script:Icons.Warning)$esc[0m Requires administrator privileges"
|
||||
return
|
||||
}
|
||||
|
||||
# Font cache locations
|
||||
$fontCachePaths = @(
|
||||
"$env:LOCALAPPDATA\Microsoft\Windows\Fonts"
|
||||
"$env:WINDIR\ServiceProfiles\LocalService\AppData\Local\FontCache"
|
||||
"$env:WINDIR\System32\FNTCACHE.DAT"
|
||||
)
|
||||
|
||||
if ($script:DryRun) {
|
||||
Write-Host " $esc[33m$($script:Icons.DryRun)$esc[0m Would stop Windows Font Cache Service"
|
||||
Write-Host " $esc[33m$($script:Icons.DryRun)$esc[0m Would delete font cache files"
|
||||
Write-Host " $esc[33m$($script:Icons.DryRun)$esc[0m Would restart Windows Font Cache Service"
|
||||
$script:RepairsApplied++
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
# Stop font cache service
|
||||
Write-Host " $esc[90mStopping Font Cache Service...$esc[0m"
|
||||
Stop-Service -Name "FontCache" -Force -ErrorAction SilentlyContinue
|
||||
Stop-Service -Name "FontCache3.0.0.0" -Force -ErrorAction SilentlyContinue
|
||||
|
||||
# Wait a moment for service to stop
|
||||
Start-Sleep -Seconds 2
|
||||
|
||||
# Delete font cache files
|
||||
foreach ($path in $fontCachePaths) {
|
||||
if (Test-Path $path) {
|
||||
if (Test-Path $path -PathType Container) {
|
||||
Get-ChildItem -Path $path -Recurse -Force -ErrorAction SilentlyContinue |
|
||||
Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
|
||||
}
|
||||
else {
|
||||
Remove-Item -Path $path -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Restart font cache service
|
||||
Write-Host " $esc[90mRestarting Font Cache Service...$esc[0m"
|
||||
Start-Service -Name "FontCache" -ErrorAction SilentlyContinue
|
||||
Start-Service -Name "FontCache3.0.0.0" -ErrorAction SilentlyContinue
|
||||
|
||||
Write-Host " $esc[32m$($script:Icons.Success)$esc[0m Font cache rebuilt successfully"
|
||||
Write-Host " $esc[90mNote: Some apps may need restart to see changes$esc[0m"
|
||||
$script:RepairsApplied++
|
||||
}
|
||||
catch {
|
||||
Write-Host " $esc[31m$($script:Icons.Error)$esc[0m Could not rebuild font cache: $_"
|
||||
Start-Service -Name "FontCache" -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
function Repair-IconCache {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Rebuild Windows icon cache
|
||||
.DESCRIPTION
|
||||
Clears the icon cache database files, forcing Windows to rebuild them.
|
||||
Fixes issues with missing, corrupted, or outdated icons.
|
||||
#>
|
||||
|
||||
$esc = [char]27
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "$esc[34m$($script:Icons.Arrow) Icon Cache Rebuild$esc[0m"
|
||||
|
||||
$iconCachePath = "$env:LOCALAPPDATA\Microsoft\Windows\Explorer"
|
||||
|
||||
if ($script:DryRun) {
|
||||
Write-Host " $esc[33m$($script:Icons.DryRun)$esc[0m Would stop Explorer"
|
||||
Write-Host " $esc[33m$($script:Icons.DryRun)$esc[0m Would delete icon cache files (iconcache_*.db)"
|
||||
Write-Host " $esc[33m$($script:Icons.DryRun)$esc[0m Would restart Explorer"
|
||||
$script:RepairsApplied++
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
Write-Host " $esc[90mStopping Explorer...$esc[0m"
|
||||
Stop-Process -Name "explorer" -Force -ErrorAction SilentlyContinue
|
||||
Start-Sleep -Seconds 2
|
||||
|
||||
# Delete icon cache files
|
||||
$iconCacheFiles = Get-ChildItem -Path $iconCachePath -Filter "iconcache_*.db" -Force -ErrorAction SilentlyContinue
|
||||
$thumbCacheFiles = Get-ChildItem -Path $iconCachePath -Filter "thumbcache_*.db" -Force -ErrorAction SilentlyContinue
|
||||
|
||||
$deletedCount = 0
|
||||
foreach ($file in $iconCacheFiles) {
|
||||
Remove-Item -Path $file.FullName -Force -ErrorAction SilentlyContinue
|
||||
$deletedCount++
|
||||
}
|
||||
|
||||
foreach ($file in $thumbCacheFiles) {
|
||||
Remove-Item -Path $file.FullName -Force -ErrorAction SilentlyContinue
|
||||
$deletedCount++
|
||||
}
|
||||
|
||||
$systemIconCache = "$env:LOCALAPPDATA\IconCache.db"
|
||||
if (Test-Path $systemIconCache) {
|
||||
Remove-Item -Path $systemIconCache -Force -ErrorAction SilentlyContinue
|
||||
$deletedCount++
|
||||
}
|
||||
|
||||
Write-Host " $esc[90mRestarting Explorer...$esc[0m"
|
||||
Start-Process "explorer.exe"
|
||||
|
||||
Write-Host " $esc[32m$($script:Icons.Success)$esc[0m Icon cache rebuilt ($deletedCount files cleared)"
|
||||
Write-Host " $esc[90mNote: Icons will rebuild gradually as you browse$esc[0m"
|
||||
$script:RepairsApplied++
|
||||
}
|
||||
catch {
|
||||
Write-Host " $esc[31m$($script:Icons.Error)$esc[0m Could not rebuild icon cache: $_"
|
||||
Start-Process "explorer.exe" -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
function Repair-SearchIndex {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Reset Windows Search index
|
||||
.DESCRIPTION
|
||||
Stops the Windows Search service, deletes the search index, and restarts.
|
||||
Fixes issues with search not finding files or returning incorrect results.
|
||||
#>
|
||||
|
||||
$esc = [char]27
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "$esc[34m$($script:Icons.Arrow) Windows Search Index Reset$esc[0m"
|
||||
|
||||
if (-not (Test-IsAdmin)) {
|
||||
Write-Host " $esc[33m$($script:Icons.Warning)$esc[0m Requires administrator privileges"
|
||||
return
|
||||
}
|
||||
|
||||
$searchIndexPath = "$env:ProgramData\Microsoft\Search\Data\Applications\Windows"
|
||||
|
||||
if ($script:DryRun) {
|
||||
Write-Host " $esc[33m$($script:Icons.DryRun)$esc[0m Would stop Windows Search service"
|
||||
Write-Host " $esc[33m$($script:Icons.DryRun)$esc[0m Would delete search index database"
|
||||
Write-Host " $esc[33m$($script:Icons.DryRun)$esc[0m Would restart Windows Search service"
|
||||
$script:RepairsApplied++
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
Write-Host " $esc[90mStopping Windows Search service...$esc[0m"
|
||||
Stop-Service -Name "WSearch" -Force -ErrorAction Stop
|
||||
Start-Sleep -Seconds 3
|
||||
|
||||
if (Test-Path $searchIndexPath) {
|
||||
Write-Host " $esc[90mDeleting search index...$esc[0m"
|
||||
Remove-Item -Path "$searchIndexPath\*" -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
Write-Host " $esc[90mRestarting Windows Search service...$esc[0m"
|
||||
Start-Service -Name "WSearch" -ErrorAction Stop
|
||||
|
||||
Write-Host " $esc[32m$($script:Icons.Success)$esc[0m Search index reset successfully"
|
||||
Write-Host " $esc[33m$($script:Icons.Warning)$esc[0m Indexing will rebuild in the background (may take hours)"
|
||||
$script:RepairsApplied++
|
||||
}
|
||||
catch {
|
||||
Write-Host " $esc[31m$($script:Icons.Error)$esc[0m Could not reset search index: $_"
|
||||
Start-Service -Name "WSearch" -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
function Repair-StoreCache {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Reset Windows Store cache
|
||||
.DESCRIPTION
|
||||
Runs wsreset.exe to clear the Windows Store cache.
|
||||
Fixes issues with Store apps not installing, updating, or launching.
|
||||
#>
|
||||
|
||||
$esc = [char]27
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "$esc[34m$($script:Icons.Arrow) Windows Store Cache Reset$esc[0m"
|
||||
|
||||
if ($script:DryRun) {
|
||||
Write-Host " $esc[33m$($script:Icons.DryRun)$esc[0m Would run wsreset.exe"
|
||||
$script:RepairsApplied++
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
Write-Host " $esc[90mResetting Windows Store cache...$esc[0m"
|
||||
$wsreset = Start-Process -FilePath "wsreset.exe" -PassThru -WindowStyle Hidden
|
||||
$wsreset.WaitForExit(30000)
|
||||
|
||||
if ($wsreset.ExitCode -eq 0) {
|
||||
Write-Host " $esc[32m$($script:Icons.Success)$esc[0m Windows Store cache reset successfully"
|
||||
}
|
||||
else {
|
||||
Write-Host " $esc[33m$($script:Icons.Warning)$esc[0m wsreset completed with code $($wsreset.ExitCode)"
|
||||
}
|
||||
$script:RepairsApplied++
|
||||
}
|
||||
catch {
|
||||
Write-Host " $esc[31m$($script:Icons.Error)$esc[0m Could not reset Store cache: $_"
|
||||
}
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Summary
|
||||
# ============================================================================
|
||||
@@ -459,12 +708,20 @@ function Show-OptimizeSummary {
|
||||
Write-Host ""
|
||||
|
||||
if ($script:DryRun) {
|
||||
Write-Host " Would apply $esc[33m$($script:OptimizationsApplied)$esc[0m optimizations"
|
||||
$total = $script:OptimizationsApplied + $script:RepairsApplied
|
||||
Write-Host " Would apply $esc[33m$total$esc[0m changes"
|
||||
if ($script:RepairsApplied -gt 0) {
|
||||
Write-Host " ($($script:OptimizationsApplied) optimizations, $($script:RepairsApplied) repairs)"
|
||||
}
|
||||
Write-Host " Run without -DryRun to apply changes"
|
||||
}
|
||||
else {
|
||||
Write-Host " Optimizations applied: $esc[32m$($script:OptimizationsApplied)$esc[0m"
|
||||
|
||||
if ($script:RepairsApplied -gt 0) {
|
||||
Write-Host " Repairs applied: $esc[32m$($script:RepairsApplied)$esc[0m"
|
||||
}
|
||||
|
||||
if ($script:IssuesFixed -gt 0) {
|
||||
Write-Host " Issues fixed: $esc[32m$($script:IssuesFixed)$esc[0m"
|
||||
}
|
||||
@@ -500,6 +757,9 @@ function Main {
|
||||
# Set dry-run mode
|
||||
$script:DryRun = $DryRun
|
||||
|
||||
# Check if any repairs were requested
|
||||
$runRepairs = $Repair -or $Font -or $Icon -or $Search -or $Store
|
||||
|
||||
# Clear screen
|
||||
Clear-Host
|
||||
|
||||
@@ -528,8 +788,30 @@ function Main {
|
||||
Test-DiskHealth
|
||||
Test-WindowsUpdate
|
||||
|
||||
# System file check is slow, ask first
|
||||
if (-not $script:DryRun -and (Test-IsAdmin)) {
|
||||
# Run repairs if requested
|
||||
if ($runRepairs) {
|
||||
Write-Host ""
|
||||
Write-Host "$esc[1;35m$($script:Icons.Arrow) Repairs$esc[0m"
|
||||
|
||||
if ($Repair -or $Font) {
|
||||
Repair-FontCache
|
||||
}
|
||||
|
||||
if ($Repair -or $Icon) {
|
||||
Repair-IconCache
|
||||
}
|
||||
|
||||
if ($Repair -or $Search) {
|
||||
Repair-SearchIndex
|
||||
}
|
||||
|
||||
if ($Repair -or $Store) {
|
||||
Repair-StoreCache
|
||||
}
|
||||
}
|
||||
|
||||
# System file check is slow, ask first (only if not doing repairs)
|
||||
if (-not $runRepairs -and -not $script:DryRun -and (Test-IsAdmin)) {
|
||||
Write-Host ""
|
||||
$runSfc = Read-Host "Run System File Checker? This may take several minutes (y/N)"
|
||||
if ($runSfc -eq 'y' -or $runSfc -eq 'Y') {
|
||||
|
||||
Reference in New Issue
Block a user