Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. function Clean-Logs {
  2. <#
  3. .SYNOPSIS
  4. Remove files oder than x days from x location
  5. .DESCRIPTION
  6. Mainly used to remove IIS logs that are older than a certain number of days.
  7. .EXAMPLE
  8. Clean-Logs -Retention 7 -Directory D:\Logs
  9. .PARAMETER Retention
  10. Number of days to retain logs. Specifying 7 will remove logs older than 7 days.
  11. .PARAMETER Directory
  12. The path to the target log directory. The cmdlet will default to C:\inetpub\logs\LogFiles.
  13. #>
  14.  
  15. [CmdletBinding()]
  16. param
  17. (
  18. [Parameter(Mandatory=$True,
  19. HelpMessage='Specify your desired retention period and log location')]
  20. [int]$Retention,
  21. [string]$Directory = 'C:\inetpub\logs\LogFiles'
  22. )
  23.  
  24. begin {
  25.  
  26. $now = Get-Date
  27. $extension = "*.log"
  28. $lastWrite = $Now.AddDays(-$retention)
  29. $files = Get-ChildItem $Directory -Include $extension -Recurse | ? {$_.LastWriteTime -le $lastWrite}
  30.  
  31. Write-Verbose -Message " - Current time: $now"
  32. Write-Verbose -Message " - File extension: $extension"
  33. Write-Verbose -Message " - Last write time: $lastWrite"
  34. Write-Verbose -Message " - Number of files $($files.count)"
  35.  
  36. }
  37.  
  38. process {
  39.  
  40. $files | ForEach-Object {
  41.  
  42. if ($_ -ne $NULL) {
  43.  
  44. Remove-Item $_.FullName | Out-Null
  45. }
  46. }
  47. }
  48.  
  49. end{}
  50.  
  51. }
  52.  
  53. #Run from a scheduled task with %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile D:\Scripts\Delete-IISLogs.ps1
  54. Clean-Logs -Retention 7 -Directory 'C:\inetpub\logs\LogFiles'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement