Skip to content

Instantly share code, notes, and snippets.

@wipash
Last active March 31, 2023 00:40
Show Gist options
  • Save wipash/6c72695d967762f8c08acb94b4ede776 to your computer and use it in GitHub Desktop.
Save wipash/6c72695d967762f8c08acb94b4ede776 to your computer and use it in GitHub Desktop.
$MaxLogAge = 31
$LogFilesDirectory = 'C:\inetpub\logs\LogFiles\'
$ErrorActionPreference = 'SilentlyContinue'
if ($LogFilesDirectory) {
Set-Location $LogFilesDirectory
# For all directories that start with "W3SVC" run the following
foreach ($LogDirectory in Get-ChildItem $LogFilesDirectory | Where-Object { $_.PsIsContainer -and $_.Name -match '^W3SVC' }) {
$WorkingDirectory = Join-Path $LogFilesDirectory $LogDirectory
Set-Location $WorkingDirectory
# Delete IIS logs older than $MaxLogAge days
Get-ChildItem $WorkingDirectory | Where-Object { $_.lastWriteTime -lt (Get-Date).AddDays(-$MaxLogAge) } | Remove-Item -Force
# Compress IIS logs older than 1 day that aren't already compressed
foreach ($Log in Get-ChildItem $WorkingDirectory | Where-Object { $_.lastWriteTime -lt (Get-Date).AddDays(-1) -and $_.Attributes -notcontains 'Compressed' }) {
$File = Join-Path $WorkingDirectory $Log.Name
$TempCmd = 'Compact /C ' + $File
Invoke-Expression -Command $TempCmd
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment