The PowerShell Community Extensions Project shipped a Write-GZip cmdlet which made me think that it's time to re-write the trusty CompressLogFiles.vbs file I've been using for a while. After grappling with the square brackets in the WMS [Global] folder I think I finally have a viable solution. Since many folks have IIS servers with logs rolling daily here's a great way to cut down on disk space (maybe I'll look at writing a module for IIS 7 next month to hook into the log rolling):
#===========================================================================
# Compress all log files older than today within a directory structure.
#===========================================================================
# Syntax: Compress-LogFiles.ps1 {LogPath}
#===========================================================================
# Revision History:
# 1.0 - 31-Jan-08 - Initial creation.
# - Colin Bowern
#===========================================================================
# Stop the script in the event of an error
$ErrorActionPreference = "Stop";
trap [System.Exception]
{
Write-Host $_.Exception.ToString();
$EventLog = New-Object System.Diagnostics.EventLog
$EventLog.Set_Log("Windows PowerShell")
$EventLog.Set_Source("PowerShell")
$EventLog.WriteEntry($_.Exception.ToString(), "Error")
}
# Load Dependencies
if (!(Test-Path Variable:__PscxProfileRanOnce))
{
Write-Error "PowerShell Community Extensions is required for this script.";
}
# Parse Command Line Arguments
if($args.length -ne 1)
{
Write-Host "NAME";
Write-Host " Compress-LogFiles.ps1";
Write-Host "";
Write-Host "SYNOPSIS";
Write-Host " Compress all log files older than today within a directory structure.";
Write-Host "";
Write-Host "SYNTAX";
Write-Host " Compress-LogFiles.ps1 {LogPath}";
Write-Host "";
Write-Host "EXAMPLE";
Write-Host " Compress-LogFiles.ps1 D:\LogFiles";
Write-Host "";
$ErrorActionPreference = "ContinueSilent";
Write-Error "Invalid number of command line arguments." -Category SyntaxError;
Break;
}
$LogPath = $args[0];
#===========================================================================
# For Each *.log File in $LogPath
$IISLogFileToday = [String]::Format("ex{0}.log", [DateTime]::Now.ToString("yyyyMMdd"));
$WMSLogFileToday = [String]::Format("wms_{0}.log", [DateTime]::Now.ToString("yyyyMMdd"));
[array]$LogFiles = Get-ChildItem -Path $LogPath -Recurse -Include "ex*.log",
"wms*.log", "````[Global````]" -Exclude $IISLogFileToday, $WMSLogFileToday,
"httperr*.log", "httperr"
if($LogFiles.Count -gt 0)
{
$Index = 0;
foreach ($LogFile in $LogFiles)
{
Write-Progress -Id 1 -activity "Compressing Files" -status $LogFile.FullName
-percentComplete($Index / $LogFiles.Count * 100);
if(Test-Path ("{0}.gz" -f ([Management.Automation.WildcardPattern]::Escape($LogFile.FullName))))
{
Write-Error "Destination file already exists." -Category ResourceExists;
}
Write-GZip ([Management.Automation.WildcardPattern]::Escape($LogFile.FullName)) -Level 9;
if(Test-Path ("{0}.gz" -f ([Management.Automation.WildcardPattern]::Escape($LogFile.FullName))))
{
Remove-Item ([Management.Automation.WildcardPattern]::Escape($LogFile.FullName))
}
$Index += 1;
}
}
else
{
Write-Host "There were no log files found that required processing.";
}
#===========================================================================