Last active
March 31, 2023 00:40
-
-
Save wipash/6c72695d967762f8c08acb94b4ede776 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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