VanillaGranilla

iisLogCleanup.ps1

May 5th, 2015
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ########################################################
  2. ## IIS Log Management
  3. ## Caution: Use at your own risk.
  4. ## No warranty expressed or implied.
  5. ## Written by: Greg Kjono on 12/1/2011
  6. ########################################################
  7.  
  8. $version = gwmi win32_operatingsystem | select version
  9. $version = $version.version.substring(0,4)
  10. $ErrorActionPreference = "Continue"
  11.  
  12. if ($version -ge "6.0."){
  13.  ## If the OS is 2k8 or higher, set this log path
  14.  [STRING]$dir = "c:\inetpub\logs\logfiles\"
  15. }else{
  16.  ## Otherwise set this log path
  17.  [STRING]$dir = $env:windir + "\system32\LogFiles\"
  18. }
  19.  
  20. if ($dir){
  21. Set-Location $dir
  22.  
  23. ## For all directories that start with "W3SVC" run the following
  24. foreach ($LogDir in Get-ChildItem $dir | Where {$_.PsIsContainer -and $_.Name -match "^W3SVC"}){
  25.  [STRING]$wrkDir =  $dir + $LogDir + "\"
  26.  Set-Location $wrkDir
  27.  
  28.  ## Delete iis logs older than 90 days
  29.  Get-ChildItem $wrkDir | where {$_.lastWriteTime -lt (Get-Date).AddDays(-90)} | Remove-Item -Force
  30.  
  31.  ## compress IIS logs older than 7 days that aren't already compressed
  32.  Foreach ($log in Get-ChildItem $wrkDir | where {$_.lastWriteTime -lt (Get-Date).AddDays(-7) -and $_.Attributes -notcontains 'Compressed'}){
  33.          $file = $wrkDir + $log.Name
  34.          $tempCmd = "Compact /C " + $file
  35.   Invoke-Expression -command $tempCmd
  36.   }
  37.  }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment