Tag Archives: Exchange2013

Clear Exchange 2013 IIS Logs

Something I’ve been working on for a while is a script to clear up all those Exchange IIS logs which are generated through time.

Why are these logs not recycled after a period of time, well for good reason in most cases is to assist with troubleshooting when something goes wrong.

Anyhow, unlike logs such as the Exchange transport log these; which are recycled after they reach a certain size. The IIS logs don’t clear down automatically.

To help clear down disk space the following script can be used. Any IIS logs older than 30 days located in the “Exchange Back end” WWW will be purged.

Download this file save as ps1 and run as administrator on your Exchange servers. I have tested this on Exchange 2013 SP1 CU6 servers.

As always please test on non-production/test servers first to check that this will work with your own environment and have a solid backup of Exchange.

<# Created by DM 151216 
Clear_Exchange_IISLogs.ps1 
got date info from here https://technet.microsoft.com/en-us/library/ff730960.aspx #>
##Variables

Write-host "Starting script"
$Retain_Logs =30
$TodaysDate = Get-Date
$ExchangeWWW = Get-Website -Name "Exchange Back End"
$ExchangeIISLog = $ExchangeWWW.logFile.directory
$LogFilesDeleted = "C:\temp\IISlogsDeleted_$(get-date -f dd-MM-yy).txt"
$DeleteOlderThan = $TodaysDate.AddDays(-$Retain_Logs)

if ($ExchangeIISLog -cge "%SystemDrive%")
        {
            $newpath = $ExchangeIISLog -replace "%SystemDrive%","C:"
            $direction = "1"
        Write-host "IIS Log folder for Exchange located here $newpath"
        }
else
        {
            $direction = "2"
            Write-host "IIS Log folder for Exchange located here $ExchangeIISLog"
        } 

##Select the right path
switch ($direction)
    {
    1
        {
        ##get list of files to delete from new path
            $LogsToDelete = Get-ChildItem $newpath -Include *.log -Recurse | Where {$_.LastWriteTime -le "$DeleteOlderThan"}
            add-content $LogFilesDeleted "Starting to delete IIS Logs files located in $IISLogsPath @ $TodaysDate" 
            add-content $LogFilesDeleted "------------------------------------------------------------------------"
        }
    2
        {
        ##get list of files to delete from orignal    
            $LogsToDelete = Get-ChildItem $ExchangeIISLog -Include *.log -Recurse | Where {$_.LastWriteTime -le "$DeleteOlderThan"}
            add-content $LogFilesDeleted "Starting to delete IIS Logs files located in $IISLogsPath @ $TodaysDate" 
            add-content $LogFilesDeleted "------------------------------------------------------------------------"
        }
    }
#Loop through each file older than value set in $Retain_Logs and record success
Write-Host "Starting to clear Logs"
foreach ($File in $LogsToDelete)
    {
        Remove-Item $File -ea SilentlyContinue
        $filestatus = Test-path $File    
            
        if ($filestatus -eq $True)
        {
            add-content $LogFilesDeleted "Failed to delete $file"
        }
        
        elseif ($filestatus -eq $False)
        {   
            add-content $LogFilesDeleted "Successfully deleted $file"
        }
    }
add-content $LogFilesDeleted "------------------------------------------------------------------------"
add-content $LogFilesDeleted "Finished deleting IIS Logs files located in $IISLogsPath @ $TodaysDate" 
add-content $LogFilesDeleted "------------------------------------------------------------------------"
Write-Host "Check $LogFilesDeleted for list of files deleted successfully and any errors"  

Script output
script execution

 

 

Sample output

Log file output

Log file output