PowerShell清空IIS日志

通过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
            }
        }
    } 
}
代码很简单,可以封装成函数使用。

你可能感兴趣的:(Windows,PowerShell)