Update: 18th Sept 2014. An updated version of this script has been written by Thomas Stensitzki and can be downloaded from http://www.sf-tools.net/Messaging/tabid/55/EntryId/213/Updated-script-to-purge-Exchange-and-IIS-log-files.aspx. This updated version works on systems that have not used the default installation paths and it reads them automatically from the server. The below still works for users with default installation paths.
Exchange 2013 creates log files for everything, and this is way more than in 2010. Everything is logged, and even when the server is doing nothing, it is recording the health of the server and writing that down to the logs.
The following PowerShell script removes log files (those named *.log) over 30 days old in the IIS logs folder and the same from the Exchange 2013 logging folder. Neither of these folders are cleaned up automatically in Exchange 2013 RTM or SP1. The transport logs in a different folder are cleaned up automatically after 30 days, so this script uses the same duration.
Set-Executionpolicy RemoteSigned -Force $days=30 #You can change the number of days here $IISLogPath="C:\inetpub\logs\LogFiles\" $ExchangeLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\Logging\" $WindowsTemp="C:\Windows\Temp\" Write-Host "Removing IIS, TMP_ and Exchange logs; keeping last" $days "days" Function CleanTmpFiles($TargetFolder) { Write-Host "Deleting tmp_* files and folders in $TargetFolder" Get-ChildItem $TargetFolder -Include tmp_* -Recurse -ErrorAction SilentlyContinue | foreach ($_) {Remove-Item $_.FullName -Recurse -ErrorAction SilentlyContinue} } Function CleanLogfiles($TargetFolder) { if (Test-Path $TargetFolder) { $Now = Get-Date $LastWrite = $Now.AddDays(-$days) $Files = Get-ChildItem $TargetFolder -Include *.log -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} foreach ($File in $Files) {Write-Host "Deleting file $File" -ForegroundColor "Red"; Remove-Item $File -ErrorAction SilentlyContinue | out-null} } Else { Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red" } } CleanLogfiles($IISLogPath) CleanLogfiles($ExchangeLoggingPath) CleanTmpFiles($WindowsTemp)
The above script runs in PowerShell and will delete logs on the server that you are running the script on.
The following version of the same script will get all the Exchange 2013 or later servers in the organization (excluding Edge Role servers) and then deletes the logs (older than 30 days) across all the servers for you from one machine. You run this second script from Exchange Management Shell (run as administrator) and need remote file access to C$ (or whichever folder you set in the script) to all the servers Exchange 2013 servers.
Set-Executionpolicy RemoteSigned -Force $days=30 #You can change the number of days here $ExchangeInstallRoot = "C" $IISLogPath="inetpub\logs\LogFiles\" $ExchangeLoggingPath="Program Files\Microsoft\Exchange Server\V15\Logging\" $WindowsTemp="Windows\Temp\" Write-Host "Removing IIS and Exchange logs; keeping last" $days "days" Function CleanTmpFiles($TargetFolder) { $TargetServerFolder = "\\$E15Server\$ExchangeInstallRoot$\$TargetFolder" Write-Host "Deleting tmp_* files and folders in $TargetServerFolder" Get-ChildItem $TargetServerFolder -Include tmp_* -Recurse -ErrorAction SilentlyContinue | foreach ($_) {Remove-Item $_.FullName -Recurse -ErrorAction SilentlyContinue} } Function CleanLogfiles($TargetFolder) { $TargetServerFolder = "\\$E15Server\$ExchangeInstallRoot$\$TargetFolder" Write-Host $TargetServerFolder if (Test-Path $TargetServerFolder) { $Now = Get-Date $LastWrite = $Now.AddDays(-$days) $Files = Get-ChildItem $TargetServerFolder -Include *.log -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} foreach ($File in $Files) { # Write-Host "Deleting file $File" -ForegroundColor "Red" Remove-Item $File -ErrorAction SilentlyContinue | out-null} } Else { Write-Host "The folder $TargetServerFolder doesn't exist! Check the folder path!" -ForegroundColor "red" } } $Ex2013 = Get-ExchangeServer | Where {($_.IsE15OrLater -eq $true) -and ($_.ServerRole -ne "Edge")} foreach ($E15Server In $Ex2013) { CleanLogfiles($IISLogPath) CleanLogfiles($ExchangeLoggingPath) CleanTmpFiles($WindowsTemp) }
Leave a Reply