准备脚本:
准备清理日志文件的脚本
# Set execution policy if not set
$ExecutionPolicy = Get-ExecutionPolicy
if ($ExecutionPolicy -ne "RemoteSigned") {Set-ExecutionPolicy RemoteSigned -Force
}# Cleanup logs older than the set of days in numbers
$days = 2# Path of the logs that you like to cleanup
$IISLogPath = "C:\inetpub\logs\LogFiles\"
$ExchangeLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
$ETLLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
$ETLLoggingPath2 = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\"# Clean the logs
Function CleanLogfiles($TargetFolder) {Write-Host -Debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolderif (Test-Path $TargetFolder) {$Now = Get-Date$LastWrite = $Now.AddDays(-$days)$Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $_.Name -like "*.log" -or $_.Name -like "*.blg" -or $_.Name -like "*.etl" } | Where-Object { $_.lastWriteTime -le "$lastwrite" } | Select-Object FullNameforeach ($File in $Files) {$FullFileName = $File.FullName Write-Host "Deleting file $FullFileName" -ForegroundColor "yellow"; Remove-Item $FullFileName -ErrorAction SilentlyContinue | out-null}}Else {Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red"}
}
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)
$days = 2 //清理超过两天的日志文件
$IISLogPath = "C:\inetpub\logs\LogFiles\"
$ExchangeLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
$ETLLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
$ETLLoggingPath2 = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\"
//若更改了Exchange日志目录,修改这里
执行脚本:
完成后,将文件保存为 CleanupLogs.ps1,保存到一个目录下。
使用管理员打开PowerShell,切换到脚本所在目录,执行该脚本
清理前磁盘空间
清理后磁盘空间
现在我们已经有一个清理日志的脚本,我们设置一下每天固定时间运行此脚本,防止磁盘空间爆满。
我们打开“任务计划程序” 控制面板----管理工具---任务计划程序
WIN+R 输入control
右键,创建基本任务/创建任务
创建基本任务:
选择触发器:
定义时间:
选择启动程序:
添加我们编写好的脚本:
确认我们的选择
查看我们的任务计划程序
总结:
及时清理Exchange日志文件是必要的,以下是清理Exchange日志文件的必要性:
-
节省磁盘空间:Exchange服务器会生成大量的日志文件,只写文件会占用大量的磁盘空间。定期清理这些日志文件可以释放磁盘空间,确保系统运行流畅
-
提升性能:大量的日志文件会增加系统的读写负载,可能导致性能下降。
-
预防日志文件溢满
-
数据保护:通过清理过时的Exchange日志文件可以减少潜在的数据泄漏风险。
注:我们清理的日志文件不是数据库日志,请放心清理!!!