Tuesday, August 2, 2011

Powershell Script to Archive IIS Log Files

Here's a script that I put together to deal with windows server log files (Mainly IIS) as previously it was a manual process for me and something I got sick of. I found scripting pieces around that were close to what I wanted but didn't fine something exactly so mashed this script together.

The script basically takes a list of servers you give it, checks each server to determine if it's 2003 or 2008, (as default IIS directories changed between 03 and 08) and then will look in the default IIS log files location for files. Anything older than the age specified it will move into a sub-folder named the year.

One shortcoming is that the script assumes there is already a folder created in your log files directory with the year. I did this as that way I could simply enable compression on those "archive" folders and then not have to deal with that in my script as it seemed to be a pain to try to enable compression using powershell. So if I wanted to put more effort into it there is certainly room for improvement.

Here's the script:

# Script to cleanup IIS files on various given servers

#define parameters
$iispath03 = "\C$\WINDOWS\system32\LogFiles\"
$iispath08 = "\C$\inetpub\logs\LogFiles\"
$OS = "default"

#set archive period after which to move logs to compressed folder
$Now = Get-Date
$Days = “8”
$LastWrite = $Now.AddDays(-$days)

#define list of IIS servers to cleanup logs on
$IISServers = @("server1","server2","server3","server4","etc","etc","etc","etc")

#Define Function to check if server 03 or 08
Function GetServerOS
{param ($strServerName)
$strCategory = "computer"
$strfilter = "(&(objectCategory=Computer)(Name=$strServerName))"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = $strfilter
$colProplist = “operatingsystem”

foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults)
{$objComputer = $objResult.Properties


#Gather list of IIS log file directories
foreach ($server in $IISServers)

#Check OS of server in question
$FullOS = GetServerOS $server
if ($FullOS[1].Contains("2003")){
$OS = "2003"
$IISDir = "\\" + $server + $iispath03
$OS = "2008"
$IISDir = "\\" + $server + $iispath08
$OS = "Error"
$IISDir = "Error"

$IISLogDirs = dir $IISDir W3* | where {$_.psIsContainer -eq $true} | select fullname

#Cycle through each of the discovered directories
foreach ($dir in $IISLogDirs)
#set current directory (should be compressed) to archive files to
$archivedir = $dir.fullname + "\" + $Now.year + "\"

#check if the current year directory exists, if not create it
if (!(Test-Path -path $archivedir))
New-Item $archivedir -type directory

#Get list of log files to cleanup
$logfiles = get-childitem $dir.fullname *.log | Where {$_.LastWriteTime -le “$LastWrite”} | select fullname
foreach ($file in $logfiles)
copy-item $file.fullname $archivedir
remove-item $file.fullname





1 comment:

Satchelmouth said...

A great help, thanks for sharing!