删除Exchange Server IIS 15天日志

删除超过15天的IIS日志

IIS日志文件默认存储在IIS服务器的%SystemDrive%\inetpub\logs\LogFiles文件夹。

通过创建一个Windows任务计划运行脚本,可以自动化删除日志文件任务。可以使用Windows Task Scheduler安排脚本在任何时间运行。

脚本从Exchange删除IIS日志。

可以改变IIS目录。

Microsoft不支持第三方删除工具

#Written by David Maugrion - ATOS - david.maugrion@atos.net
#change the d value to match your policy

$d=-15
$report = Get-ClientAccessServer | Where-object {Test-Connection -ComputerName $_.Name -Count 1 -Quiet} | Foreach-Object {
    $cas = $_.Name
    Write-Verbose "Making IIS connection to $cas" 
    ([adsi]"IIS://$cas/W3SVC").Children | Where-Object {$_.KeyType -eq 'IIsWebServer'} | Foreach-Object {
        New-Object -TypeName PSObject -Property @{
            #WebSite = $_.ServerComment[0]
            LogLocation = $_.LogFileDirectory[0]
            UncLogLocation = $_.LogFileDirectory[0] -replace "^(.)(.)","\\$cas\`$1$"
        }
    }
}


#display the results
$report

$report | Foreach-Object{
   
gci -Path $_.UncLogLocation -recurse -include *.log -Force | where -FilterScript {$_.LastWriteTime -le [System.DateTime]::Now.AddDays($d)} | Ri -Force -Confirm:$false

$t=(gci -Path $_.UncLogLocation -recurse -include *.log -Force | where -FilterScript {$_.LastWriteTime -le [System.DateTime]::Now.AddDays($d)}).Count
$gt=$gt+$t
}
if ($gt -gt 0) 
{Write-Host "$gt Log files deleted" -foregroundcolor yellow}
else
{Write-Host "No Log files deleted" -foregroundcolor yellow}