-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🆕 Start writing script to clean IIS logs
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
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
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 { | ||
|
||
} | ||
} |