Automated clean up and archive of log files with PowerShell 5
Archiving old log files with PowerShell 5 and Windows Scheduled Tasks
Depending on the application architecture, your Windows virtual machine might have one or more applications or components of a single solution. Those might be WebApi services, Web Applications or Windows applications.
Any of these components may generate logs which are important for diagnostics and tracking down events. These logs are very often plan text files written to the disk of the Windows system where it is running.
By default IIS is generating file logs for each request for each website running on it. More info https://msdn.microsoft.com/en-us/library/ms525410(v=vs.90).aspx
These logs storage is often unmanaged and keep pileing up on the disk. In one of my previous article Clean up IIS log files from web server using PowerShell you can find PowerShell script which is taking care of the old IIS log files by deleting them. It is fetching log paths for each website on local IIS and deleting old files based on the defined file age.
However if you have custom locations for your application which might be Windows Service, this will not work as you need to perform actions on the specific folder path. Another thing that this script does no do is archiving of old logs. It is just deleting them and sometime they might be useful in tracking down issues which occured in the past. This is the reason I wrote this script which is not only deleting files, but it archives them first.
Before you continue check if you have PowerShell 5 installed on your system by running the following command.
Write-Output $psversiontable
In case you have older version of PowerShell you can download it from Microsoft official download link https://www.microsoft.com/en-us/download/details.aspx?id=50395
At the beginning of the script we need to declare few things:
$logFolder = "C:\temp\log" #Folder path of the log files $fileAge = 7 #How old log files to be to archive them (days) $archiveAge = 30 #How old archive files to be old to delete them (days)
Once we have these three variables we can start performing operations on log files:
- Archive old log files
- Delete archived fies
- Delete old archive files
$logFolder = "C:\temp\log" $fileAge = 7 $archiveAge = 30 $logFiles = Get-ChildItem $logFolder -Filter *.log | Where LastWriteTime -lt (Get-Date).AddDays(-1 * $fileAge) $destinationPath = $logFolder (Get-Date -format "yyyyMMddHHmmss") ".zip" $logFilePaths = @() foreach($logFile in $logFiles){ $logFilePaths = $logFile.FullName } Compress-Archive -Path $logFilePaths -DestinationPath $destinationPath -CompressionLevel Optimal Remove-Item –path $logFilePaths $archiveFiles = Get-ChildItem $logFolder -Filter *.zip | Where LastWriteTime -lt (Get-Date).AddDays(-1 * $archiveAge) foreach($archiveFile in $archiveFiles){ Remove-Item –path $archiveFile.FullName }
To have things completely automated, this script needs to be triggered from time to time. For this Windows has out of the box Scheduled Tasks. You can easily create a new task pointing to your PowerShell script file.
Since you might have more than one components of your solution that can log to different locations on the same machine, it is not so suitable to use separate file with just different path of file age variables for each task you might have per component. For this situation you can modify script variables to be parameters which are passed to the script
Param( [Parameter(Mandatory=$true)] [string]$logFolder, [Parameter(Mandatory=$true)] [int]$fileAge, [Parameter(Mandatory=$true)] [int]$archiveAge )
After modifying script you can easily invoke it with the parameters
powershell.exe -file C:\Scripts\Zip-Old-Log-Files.ps1 -logFolder C:\temp\log\ -fileAge 7 -archiveAge 30
References
Disclaimer
Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.
Comments for this article