Created
April 7, 2021 14:49
-
-
Save andreagx/3ed47dec1ef740e86c2af90ef5534dd4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Set execution policy | |
| $ExecutionPolicy = Get-ExecutionPolicy | |
| if ($ExecutionPolicy -ne "RemoteSigned") { | |
| Set-ExecutionPolicy RemoteSigned -Force | |
| } | |
| # Cleanup logs, change number of days as you want | |
| $days = 7 | |
| # Path of the logs to cleanup | |
| $ETLLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\" | |
| $ETLLoggingPath2 = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\" | |
| $IISLogPath = "C:\inetpub\logs\LogFiles\" | |
| $ExchangeLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Logging\" | |
| # Clean the logs | |
| Function CleanLogfiles($TargetFolder) { | |
| Write-Host -Debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder | |
| if (Test-Path $TargetFolder) { | |
| $Now = Get-Date | |
| $LastWrite = $Now.AddDays(-$days) | |
| $Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $_.Name -like "*.log" -or $_.Name -like "*.blg" -or $_.Name -like "*.etl" } | Where-Object { $_.lastWriteTime -le "$lastwrite" } | Select-Object FullName | |
| foreach ($File in $Files) { | |
| $FullFileName = $File.FullName | |
| Write-Host "Deleting file $FullFileName" -ForegroundColor "yellow"; | |
| Remove-Item $FullFileName -ErrorAction SilentlyContinue | out-null | |
| } | |
| } | |
| Else { | |
| Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red" | |
| } | |
| } | |
| CleanLogfiles($IISLogPath) | |
| CleanLogfiles($ExchangeLoggingPath) | |
| CleanLogfiles($ETLLoggingPath) | |
| CleanLogfiles($ETLLoggingPath2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment