Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $LogPath = "C:inetpublogs"
- $maxDaystoKeep = -30
- $outputPath = "c:CleanupTaskCleanup_Old_logs.log"
- $itemsToDelete = dir $LogPath -Recurse -File *.log | Where LastWriteTime -lt ((get-date).AddDays($maxDaystoKeep))
- if ($itemsToDelete.Count -gt 0){
- ForEach ($item in $itemsToDelete){
- "$($item.BaseName) is older than $((get-date).AddDays($maxDaystoKeep)) and will be deleted" | Add-Content $outputPath
- Get-item $item | Remove-Item -Verbose
- }
- }
- ELSE{
- "No items to be deleted today $($(Get-Date).DateTime)" | Add-Content $outputPath
- }
- Write-Output "Cleanup of log files older than $((get-date).AddDays($maxDaystoKeep)) completed..."
- start-sleep -Seconds 10
- Get-item : Cannot find path 'C:UsersAdministratoru_ex160814.log' because it does not exist.
- At line:10 char:9
- + Get-item $item | Remove-Item -Verbose
- + ~~~~~~~~~~~~~~
- + CategoryInfo : ObjectNotFound: (C:UsersAdministratoru_ex160814.log:String) [Get-Item], ItemNotFoundExcept
- ion
- + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
Add Comment
Please, Sign In to add comment