通过Powershell可以很方便地清理IIS日志,今天获得了一个脚本,现在贴出来作为学习记录。
# Module: Powershell script to clean IIS log files
Set-Executionpolicy RemoteSigned
$days=-7
(Get-Variable Path).Options="ReadOnly"
$Path="C:\inetpub\logs\LogFiles\W3SVC1"
Write-Host "Removing IIS-logs keeping last" $days "days"
CleanTempLogfiles($Path)
function CleanTempLogfiles()
{
param ($FilePath)
Set-Location $FilePath
Foreach ($File in Get-ChildItem -Path $FilePath)
{
if (!$File.PSIsContainerCopy)
{
if ($File.LastWriteTime -lt ($(Get-Date).Adddays($days)))
{
remove-item -path $File -force
Write-Host "Removed logfile: " $File
}
}
}
}
代码很简单,可以封装成函数使用。
本文介绍了一个简单的Powershell脚本,用于清理IIS日志文件,保留最近7天的日志记录。该脚本可以方便地应用于服务器维护工作中。
2319

被折叠的 条评论
为什么被折叠?



