Guest User

Untitled

a guest
Nov 13th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. $LogPath = "C:inetpublogs"
  2. $maxDaystoKeep = -30
  3. $outputPath = "c:CleanupTaskCleanup_Old_logs.log"
  4.  
  5. $itemsToDelete = dir $LogPath -Recurse -File *.log | Where LastWriteTime -lt ((get-date).AddDays($maxDaystoKeep))
  6.  
  7. if ($itemsToDelete.Count -gt 0){
  8. ForEach ($item in $itemsToDelete){
  9. "$($item.BaseName) is older than $((get-date).AddDays($maxDaystoKeep)) and will be deleted" | Add-Content $outputPath
  10. Get-item $item | Remove-Item -Verbose
  11. }
  12. }
  13. ELSE{
  14. "No items to be deleted today $($(Get-Date).DateTime)" | Add-Content $outputPath
  15. }
  16.  
  17. Write-Output "Cleanup of log files older than $((get-date).AddDays($maxDaystoKeep)) completed..."
  18. start-sleep -Seconds 10
  19.  
  20. Get-item : Cannot find path 'C:UsersAdministratoru_ex160814.log' because it does not exist.
  21. At line:10 char:9
  22. + Get-item $item | Remove-Item -Verbose
  23. + ~~~~~~~~~~~~~~
  24. + CategoryInfo : ObjectNotFound: (C:UsersAdministratoru_ex160814.log:String) [Get-Item], ItemNotFoundExcept
  25. ion
  26. + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
Add Comment
Please, Sign In to add comment