From 7ac8da84a188ff07962ab5119439b2409d94b9cd Mon Sep 17 00:00:00 2001 From: max Date: Fri, 18 Apr 2025 15:37:17 +0000 Subject: [PATCH] =?UTF-8?q?cleanExchangeLogs.ps1=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cleanExchangeLogs.ps1 | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 cleanExchangeLogs.ps1 diff --git a/cleanExchangeLogs.ps1 b/cleanExchangeLogs.ps1 new file mode 100644 index 0000000..3c52cc8 --- /dev/null +++ b/cleanExchangeLogs.ps1 @@ -0,0 +1,41 @@ +$executionPolicy = Get-ExecutionPolicy +if ($executionPolicy -ne 'RemoteSigned') { + Set-Executionpolicy RemoteSigned -Force +} + +$days = 2 +$IISLogPath = "C:\inetpub\logs\LogFiles\" +$ExchangeLoggingPath = "C:\Program Files\Microsoft\Exchange Server\V15\Logging\" +$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" + +Function CleanLogfiles($TargetFolder) +{ + Write-Host -ForegroundColor Yellow -BackgroundColor Black $TargetFolder + + if (Test-Path $TargetFolder) { + $Now = Get-Date + $LastWrite = $Now.AddDays(-$days) + $Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $_.Extension -in '.log', '.blg', '.etl' -and $_.LastWriteTime -le $lastwrite } | Select-Object -ExpandProperty FullName + + foreach ($File in $Files) + { + Write-Host "Deleting file $File" -ForegroundColor "yellow"; + try { + Remove-Item $File -ErrorAction Stop + } + catch { + Write-Warning -Message $_.Exception.Message + } + + } + } + else { + Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red" + } +} + +CleanLogfiles($IISLogPath) +CleanLogfiles($ExchangeLoggingPath) +CleanLogfiles($ETLLoggingPath) +CleanLogfiles($ETLLoggingPath2) \ No newline at end of file