Skip to content

Commit

Permalink
🆕 Start writing script to clean IIS logs
Browse files Browse the repository at this point in the history
  • Loading branch information
SamErde committed Dec 22, 2023
1 parent 4f260f4 commit 1969b23
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Clean-IISLogs.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# If the WebAdministration module is installed:
$WebSites = Get-WebSite
foreach ($item in $WebSites) {
$LogFileFolder = [string]($($WebSite.logFile.directory)\w3svc$($WebSite.id)).replace("%SystemDrive%",$env:SystemDrive)

# Create verbose and info output for this step
Remove-OldFiles -Path $LogFileFolder -Days 60
}

# Alternative approach to test
$WebSites = Get-ChildItem IIS:\Sites
foreach ($item in $WebSites) {
$LogFileFolder = $($item.logfile.directory) + "\w3svc" + $($item.id)

# Create verbose and info output for this step
Remove-OldFiles -Path $LogFileFolder -Days 60
}

# Check default log file location without requiring the WebAdministration module
$DefaultIISLogLocation = "$env:SystemDrive\inetpub\logs\LogFiles"
if (Test-Path -Path $DefaultIISLogLocation) {
Remove-OldFiles -Path $DefaultIISLogLocation -Days 60
}

# Check registry for log file lcoation without requiring the WebAdministration module
$LogDir = Get-ItemProperty -Path "HKLM:\\System\\CurrentControlSet\\Services\\InetInfo\\Parameters" -Name "LogDir" | Select-Object -ExpandProperty LogDir
if (Test-Path -Path $LogDir) {
Remove-OldFiles -Path $DefaultIISLogLocation -Days 60
} else {
# Add some info and verbose messaging
}

function Remove-OldFiles {
[CmdletBinding()]
param (
[parameter]
$Path,

[parameter]
[int]$Days = 60
)

begin {

}

process {
Get-ChildItem -Path $Path | Where-Object {$_.CreationTime -le ([datetime]::Now.AddDays( -$Days ))} | Remove-Item
}

end {

}
}

0 comments on commit 1969b23

Please sign in to comment.