CleanUp-IISLogs

Find IIS logfiles location (WMI request on IISWebServerSetting class) and clean them: delete files older than x days and compress (without any external tool) files older than y days

<#
.SYNOPSIS
    CleanUp-IISLogs.ps1 - Find IIS logfiles location and clean them (delete and compress)
.DESCRIPTION
    CleanUp-IISLogs - Find IIS logfiles location (WMI)
    and clean them (delete files older than x days and compress files older than y days)
.PARAMETER DeleteOlderThan
    Defines the number of days (LastWriteTime) after what the logfiles will be deleted
    Default is "30".
.PARAMETER CompressOlderThan
    Defines the number of days (LastWriteTime) after what the logfiles will be compressed
    Default is "1".
.PARAMETER ShowErrors
    Switches the 'ErrorActionPreference' between "Continue" and "SilentlyContinue"
    Default is "no".
.NOTES
    File Name   : CleanUp-IISLogs.ps1
    Author      : Fabrice ZERROUKI - fabricezerrouki@hotmail.com
.EXAMPLE
    PS D:\> .\CleanUp-IISLogs.ps1 -DeleteOlderThan 60 -CompressOlderThan 10
    Finds all IIS logfiles, deletes files older than 2 months (60 days) and compresses files older than 10 days
#>
Param(
    $DeleteOlderThan="30",
    $CompressOlderThan="1",
    [ValidateSet( "yes", "no")]
    [string]$ShowErrors="no"
    )
if ($CompressOlderThan -ge $DeleteOlderThan)
{
Write-Host "`nThe `"DeleteOlderThan`" parameter must be greater than the `"CompressOlderThan`" parameter ya silly cow!" -ForegroundColor DarkRed
}
else 
{
if ($ShowErrors -eq "yes") {
$global:ErrorActionPreference="Continue"
} else {
$global:ErrorActionPreference="SilentlyContinue"
}

function New-Zip
{
	param([string]$zipfilename)
	set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
	(dir $zipfilename).IsReadOnly = $false
}

function Add-Zip
{
	param([string]$zipfilename)

	if(-not (test-path($zipfilename)))
	{
		set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
		(dir $zipfilename).IsReadOnly = $false	
	}
	
	$shellApplication = new-object -com shell.application
	$zipPackage = $shellApplication.NameSpace($zipfilename)
	
	foreach($file in $input) 
	{ 
            $zipPackage.CopyHere($file.FullName)
            Start-sleep -milliseconds 500
	}
}

$Now=Get-Date
$LastWriteDelete=$Now.AddDays(-$DeleteOlderThan)
$LastWriteCompress=$Now.AddDays(-$CompressOlderThan)

$LogLocation=Get-WmiObject -Namespace 'root\MicrosoftIISv2' -Class IISWebServerSetting

ForEach ($Log in $LogLocation)
{
$RootPath=$Log | % {$_.LogFileDirectory}
$WebsiteID=$Log | % {$_.Name}
$WebsiteID=$WebsiteID.Replace("W3SVC/", "W3SVC")

if (Test-Path $RootPath\$WebsiteID) {
$Files=(Get-ChildItem $RootPath\$WebsiteID | Where-Object {-not ($_.PSIsContainer)}).Count
} else {
$Files=0
}
Write-Host "`n$files logfiles found in $RootPath\$WebsiteID" -ForegroundColor DarkGreen;
$CandidatesToDeletion=Get-Childitem $RootPath\$WebsiteID -Recurse | Where {$_.LastWriteTime -le "$LastWriteDelete" -and -not ($_.PSIsContainer)}
$CandidatesToDeletionNB=$CandidatesToDeletion.Count
Write-Host "`n`t> $CandidatesToDeletionNB logfiles in $RootPath\$WebsiteID are candidates to deletion (older than $DeleteOlderThan days.)" -ForegroundColor DarkYellow;
ForEach($CandidateToDeletion in $CandidatesToDeletion)
    {
    Remove-Item $RootPath\$WebsiteID\$CandidateToDeletion
    Write-Host "Info: $RootPath\$WebsiteID\$CandidateToDeletion deleted"
    }
    Start-Sleep 2

$CandidatesToCompression=Get-Childitem $RootPath\$WebsiteID -Recurse | Where {$_.LastWriteTime -le "$LastWriteCompress" -and -not ($_.PSIsContainer)}
$CandidatesToCompressionNB=$CandidatesToCompression.Count
Write-Host "`n`t> $CandidatesToCompressionNB logfiles in $RootPath\$WebsiteID are candidates to compression (older than $CompressOlderThan days.)`n" -ForegroundColor DarkYellow;
ForEach($CandidateToCompression in $CandidatesToCompression)
    {
    $ZipName="$RootPath\$WebsiteID\$CandidateToCompression" + ".zip"
    New-Zip $ZipName
    $FileName="$RootPath\$WebsiteID\$CandidateToCompression"
    $FileName | Add-Zip $ZipName
    Remove-Item $FileName
    Write-Host "Info: $RootPath\$WebsiteID\$CandidateToCompression compressed"
    }
}
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top