Clearing Logs from All Exchange Servers

Here’s a fun script. There are plenty of scripts that clear logs from an Exchange server, but this one goes the extra mile by doing it on every Exchange server in your environment (CAS, HUB, and MBX). The short explanation for why is that I work with 16+ node Exchange deployments, so setting up a single-server script on multiple servers is a huge pain. I imagine other people are dealing with that as well.

The script will pass through a list directories that are stored in a hash table and delete all .log files in that directory and all child directories, based on the age of the file (older than 7 days by default).

This script is *not* meant to clear Transaction Logs and should not be configured to do so, though it is certainly possible to configure it to do so. You’ve been warned.

#This line will check to determine if the Exchange Snapin is added. If not, it will add it. For other Exchange versions, change to match your version's snapin name.
if((get-pssnapin | where {$_.name -eq "microsoft.exchange.management.powershell.snapin"}) -eq $null){Add-PSSnapin microsoft.exchange.management.powershell.snapin}

#Pulls a list of Exchange servers.
$servers= Get-exchangeserver

#This foreach loop will pass through the list of servers one at a time and run an invoke-command command against each server. The invoke-command script
#uses a hash table that holds each log file folder.
foreach ($server in $servers)
{
    #This command will run a script that cycles through a Hashtable of paths to pass to a delete command. Change the file paths to match your environment.
    #Current paths are Exchange and IIS defaults.
    Invoke-Command -ComputerName $server -ScriptBlock {
        #Number of days worth of files you want to retain when the script runs. This value should be negative because the .AddDays() method doesn't do subtraction,
        #and there is no .RemoveDays() method. So if you wanted to keep 14 days of files, you would set this value to -14. Default is -7.
        $x = -7
        #User Instruction - Change $dirnumber to match the number of directories you would like the script to clear.
        $dirnumber = 3
        #This hashtable is used to store paths where you would like to delete files. Hashtable starts with 3 entries, add more by appending a comma, then
        #the path in quotations. Be sure to add the file extension (*.log for log files) that you want to erase to avoid potential disaster. Also, don't do transaction logs.
        $dirs = @{dir="C:\Program Files\Microsoft\Exchange Server\V15\Logging\*.log","C:\inetpub\logs\LogFiles\W3SVC1\*.log","C:\inetpub\logs\LogFiles\W3SVC2\*.log"}
        #Simple counter object to keep track of which cycle the deletion script is on.
        $i=0
        #This is the loop that does all the magic. As long as the cycle number ($i) is less than the number of directories in the $dirs hashtable,
        #the loop will continue cycling, finding and removing files based on
        while ($i -lt $dirnumber){
            $files = dir $dirs.dir[$i] -recurse | where {$_.LastWriteTime -lt ((get-date).AddDays($x))} | Remove-Item -Confirm:$false -force -ErrorAction SilentlyContinue
            $i++
        }
    }
}
Advertisements