
























# AutoCleanC_DeepScan.ps1
# Windows Server 2012 C盘清理 + 深层扫描增强版
$LogDir = "C:\CleanCDrive"
$LogFile = "$LogDir\CleanLog.txt"
$ReportFile = "$LogDir\SpaceReport.txt"
If (!(Test-Path $LogDir)) { New-Item -ItemType Directory -Path $LogDir }
$IISLogKeepDays = 30
$LargeFileThresholdMB = 300
Function Write-Log {
Param ([string]$Message)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "$timestamp - $Message"
Add-Content -Path $LogFile -Value $logEntry
Write-Host $logEntry
}
Function Write-Report {
Param ([string]$Message)
Add-Content -Path $ReportFile -Value $Message
}
Write-Log "=================== C盘清理与深层分析开始 ==================="
# --------------------------
# 清理操作
# --------------------------
Try { Remove-Item "$env:windir\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue; Write-Log "Windows 临时文件清理完成" } Catch { Write-Log "清理 Windows 临时文件失败: $_" }
Try { Remove-Item "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue; Write-Log "用户临时文件清理完成" } Catch { Write-Log "清理用户临时文件失败: $_" }
Try { RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255; Write-Log "IE缓存清理完成" } Catch { Write-Log "清理 IE缓存失败: $_" }
Try { Stop-Service wuauserv -Force; Remove-Item "$env:SystemRoot\SoftwareDistribution\Download\*" -Recurse -Force -ErrorAction SilentlyContinue; Start-Service wuauserv; Write-Log "Windows更新缓存清理完成" } Catch { Write-Log "清理 Windows更新缓存失败: $_" }
Try { Clear-RecycleBin -Force -ErrorAction SilentlyContinue; Write-Log "回收站清理完成" } Catch { Write-Log "清理回收站失败: $_" }
# 清理 IIS 日志
Try {
$IISLogPath = "C:\inetpub\logs\LogFiles"
If (Test-Path $IISLogPath) {
$cutoffDate = (Get-Date).AddDays(-$IISLogKeepDays)
Get-ChildItem -Path $IISLogPath -Recurse -File | Where-Object { $_.LastWriteTime -lt $cutoffDate } | ForEach-Object {
Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue
Write-Log "删除 IIS日志: $($_.FullName)"
}
Write-Log "IIS日志清理完成(保留最近 $IISLogKeepDays 天)"
} Else { Write-Log "未找到 IIS日志路径: $IISLogPath" }
} Catch { Write-Log "清理 IIS日志失败: $_" }
# --------------------------
# 深层扫描 C盘重要目录
# --------------------------
Write-Report "=================== C盘空间深层分析 ==================="
Write-Report "分析时间: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
$drive = Get-PSDrive C
Write-Report "C盘总大小: $([math]::Round($drive.Used/1GB + $drive.Free/1GB,2)) GB"
Write-Report "已用空间: $([math]::Round($drive.Used/1GB,2)) GB"
Write-Report "可用空间: $([math]::Round($drive.Free/1GB,2)) GB"
Write-Report "`r`n"
# 定义需要扫描的重要目录
$scanDirs = @(
"C:\Windows",
"C:\ProgramData",
"C:\Users",
"C:\inetpub\logs\LogFiles"
)
# 遍历重要目录统计大小
foreach ($dir in $scanDirs) {
Try {
$size = (Get-ChildItem $dir -Recurse -File -ErrorAction SilentlyContinue | Measure-Object Length -Sum).Sum
Write-Report "$([math]::Round($size/1GB,2)) GB - $dir"
} Catch {
Write-Log "计算文件夹大小失败: $dir"
}
}
# --------------------------
# 数据库目录占用报告(可选,InfluxDB / 其他数据库)
# --------------------------
$DBDirs = @("C:\Users\Administrator\.influxdbv2")
foreach ($db in $DBDirs) {
if (Test-Path $db) {
Try {
$size = (Get-ChildItem $db -Recurse -File -ErrorAction SilentlyContinue | Measure-Object Length -Sum).Sum
Write-Report "$([math]::Round($size/1GB,2)) GB - 数据库目录: $db"
} Catch {
Write-Log "计算数据库目录大小失败: $db"
}
}
}
# --------------------------
# 扫描大文件(>300 MB)
# --------------------------
Write-Report "`r`nC盘大文件(>$LargeFileThresholdMB MB):"
foreach ($dir in $scanDirs + $DBDirs) {
Try {
Get-ChildItem $dir -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $_.Length -gt ($LargeFileThresholdMB *1MB) } | Sort-Object Length -Descending | ForEach-Object {
$sizeGB = [math]::Round($_.Length/1GB,2)
Write-Report "$sizeGB GB - $($_.FullName)"
}
} Catch {
Write-Log "扫描大文件失败: $dir"
}
}
Write-Log "C盘清理与深层分析完成"
Write-Report "=================== 分析结束 ===================`r`n"
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。