Maintain-IISLogs.ps1
Description:
Script will remove logs in the IIS logging directory over x days old.
Language:
PowerShell
Usage Type:
Standalone
Script Source:
##################################### # Maintain IIS Logs # Author: Sean Huggans # Script Date: 18.12.24.1 ##################################### $LogDir = "C:\inetpub\logs" $LogPath = "$($LogDir)\Log-Maintenance.log" $ArchivedLogRetentionPeriod = 5 # In Days ################### # Functions ################### function Log-Action ($Message) { if (!(test-path -Path $LogDir)) { New-Item -ItemType Directory -Path $LogDir -Confirm:$false -Force | Out-Null } "[ $(Get-Date -Format 'yyyy.MM.dd HH:mm:ss') ] $($Message)" | Out-File -FilePath $LogPath -Force -Append } function Maintain-Logs { Remove-Item -Path $LogPath -ErrorAction SilentlyContinue -Force -confirm:$false Log-Action "Log Maintenance - Beginning Log Maintenance!" # If any Archived Logs are older than Retention Period days $CutOffNumber = "-$($ArchivedLogRetentionPeriod)" $Cutoff = (Get-Date).AddDays($CutOffNumber) [array]$ArchivedLogs = Get-ChildItem -Path $LogDir -Recurse | Where-Object {$_.Extension -eq ".log"} if ($ArchivedLogs.Count -gt 0) { foreach ($ArchivedLog in $ArchivedLogs) { if ($ArchivedLog.LastWriteTime -lt $Cutoff) { try { remove-item -Path $ArchivedLog.FullName -Confirm:$False -force -erroraction Stop | Out-Null Log-Action "Log Maintenance - Deleted $($ArchivedLog.Name) due to exceeding the retention period of $($ArchivedLogRetentionPeriod) days." } catch { Log-Action "Log Maintenance - Failed to delete $($ArchivedLog.Name). This file needs to be deleted (manually if needed) due to exceeding the retention period of $($ArchivedLogRetentionPeriod) days." } } else { Log-Action "Log Maintenance - $($ArchivedLog.Name) will be retained as its age is within the retention period of $($ArchivedLogRetentionPeriod) days." } } } else { Log-Action "Log Maintenance - No old Logs Exist to Evaluate." } Log-Action "Log Maintenance - Log Maintenance Completed." } Maintain-Logs
Note: that all applications posted here are posted for use, both commercial and non-commercial, free of charge, and as such are provided as-is, without warranty of any kind whatsoever. visuaFUSION, FMSCUG or any other program listed here's author are not responsible for any damages or shortcomings that result from usage of any of these applications.