我们都知道Exchange 2013服务器上会定期产生很大的日志(例如:Diagnostic Logs (Health Manager) 、ETL Files、IIS Logs、HTTP Proxy Logs)。这些日志如果不定期删除,将会一直保留下来,这样会导致服务器磁盘空间不断减小,需要Exchange管理员定期对日志文件进行删除。手动删除操作繁琐,下面介绍一个自己改造后的脚本给大家。微软官网也有类似的脚本可以参考:http://social.technet.microsoft.com/wiki/contents/articles/31117.exchange-2013-logging-clear-out-the-log-files.aspx

我在脚本中加入自动识别Exchange安装目录,而不用手动输入。另外该脚本只适用于Exchange 2013版本服务。

脚本内容如下:

---------------------------------脚本开始,将脚本内容另存为.ps1----------------------------------------------

Set-Executionpolicy RemoteSigned  
$days=0    
$IISLogPath="C:\inetpub\logs\LogFiles\"    
$exinstallpath=$env:ExchangeInstallPath 
$ExchangeLoggingPath="$exinstallpath\Logging\"    
$ETLLoggingPath="$exinstallpath\Bin\Search\Ceres\Diagnostics\ETLTraces\"    
$ETLLoggingPath2="$exinstallpath\Bin\Search\Ceres\Diagnostics\Logs"    
Function CleanLogfiles($TargetFolder)    
{    
    if (Test-Path $TargetFolder) {    
        $Now = Get-Date    
        $LastWrite = $Now.AddDays(-$days)    
        $Files = Get-ChildItem $TargetFolder -Include *.log,*.blg, *.etl, *.txt –Recurse –Force  | Where {$_.LastWriteTime -le "$LastWrite"}    
        foreach ($File in $Files)    
            {Write-Host "Deleting file $File" -ForegroundColor "white"; Remove-Item $File -ErrorAction SilentlyContinue | out-null}    
       }    
Else {    
            Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "white"    
    }    
}    
CleanLogfiles($IISLogPath)    
CleanLogfiles($ExchangeLoggingPath)    
CleanLogfiles($ETLLoggingPath)    
CleanLogfiles($ETLLoggingPath2)

---------------------------------------------------------脚本结束-----------------------------------------------------------------