PS 2.0 – Remove and compress IIS logs automatically

I created a PS script for removing and compressing IIS log files.

Description

  • Define variables: the log folder $LogFolder (basically it could be %SYSTEMROOT%\System32\LogFiles\W3SVC) and the retention periods ($DeletionRetention = -120, $CompressionRetention = -60).
  • Scripts handles only files with expired retention for attribute LastWriteTime  (e.g. -120 = older than 120 days).
  • It deletes all* files with expired retention ($DeletionRetention) from the log folder. (* Be careful, the folder doesn’t have to contain only logs.)
  • It compresses log files with expired retention ($CompressionRetention) into one zip based on month number from LastWriteTime (IISLogs-Month2-2502132135.zip). Number 2502132135 is the time stamp.
  • The script can be simply scheduled via Windows Task Scheduler if needed (e.g. Trigger: Monthly – last day).HowToScheduleScript

Note

I used in my script zip functions from David Aiken – Compress Files with Windows PowerShell then package a Windows Vista Sidebar Gadget

Script

# VARIABLES
$LogPath = "D:\IISLogs"
$DeletionRetention = -120
$CompressionRetention = -60

# FUNCTIONS
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
 }
}

function Get-Zip
{
 param([string]$zipfilename)
 if(test-path($zipfilename))
 {
 $shellApplication = new-object -com shell.application
 $zipPackage = $shellApplication.NameSpace($zipfilename)
 $zipPackage.Items() | Select Path
 }
}

# MAIN SCRIPT
$Items = get-childitem $LogPath

$DeleteLogs = $Items | ? {$_.LastWriteTime -le (Get-Date).adddays($DeletionRetention)}
if($DeleteLogs -ne $null) { $DeleteLogs | %{Remove-Item $_.fullname}}

$Items = get-childitem $LogPath |? {($_.extension -like "*.log") -and ($_.LastWriteTime -le (Get-Date).adddays($CompressionRetention))}

$GroupedItems = $Items | sort LastWriteTime | select @{n='Month';e={$_.LastWriteTime.month}},fullname,length,name | group month

if($GroupedItems -ne $null){
 foreach ($GroupedItem in $GroupedItems){
 $MonthNumber = $GroupedItem.Name
 $Time = Get-Date -Format ddMMyyHHss
 $ZipPath = "$LogPath\IISLogs-Month$MonthNumber-$Time.zip"
 New-Zip $ZipPath
 $GroupedItem.group | select fullname,length | %{
 Get-Item $_.fullname | Add-Zip $ZipPath
 start-sleep -s ($_.Length/20000000)
 }
 if((Get-Zip $ZipPath).length -eq $GroupedItem.Count){
 $GroupedItem.group | %{Remove-Item $_.fullname}
 }else{
 Write-Host "`nERROR - Files are not zipped correctly. Deletion process skipped."
 $GroupedItem.group | %{$_.name}
 }
 }
}

Download

IISLog-Removing&Compressing-v1.ps1

https://skydrive.live.com/redir?resid=E3BA57A6A24B6F3C!137

Note

We can remove logs also by command Forfiles (thank you Lukas).

 

 

Advertisements