Clear-ExchangeLogs.psm1

#TITLE Clear-ExchangeLogs
#AUTHOR Rowel Balot
#DATE February 16, 2019

Function Clear-ExchangeLogs {
Param(

    [Parameter(Mandatory=$false,Position=0)]
    [string]$Days="3",

    [Parameter(Mandatory=$false,Position=1)]
    [ValidateSet("2010","2013","2016","2019")]
    [string]$ExchangeVersion="2016"
)

$ExchangeLogs = Get-ExchangeLogPath -EXVersion $ExchangeVersion

$Age = (Get-DAte).AddDays(-$Days)

$ExchangeLogs.PSObject.Properties | ForEach-Object {
    if (Test-Path $_.Value) {
        $Files = Get-ChildItem $_.Value -Include *.log,*.blg, *.etl, *.txt -Recurse | Where {$_.LastWriteTime -le "$Age"}
        
        foreach ($File in $Files) {
            Write-Host "Purging $File" -ForegroundColor Green; Remove-Item $File -ErrorAction SilentlyContinue > $null}
    }
    else {
        Write-Host $_.Name ":" $_.Value "- folder doesn't exist!" -ForegroundColor Yellow
    }

}
}

Function Get-ExchangeLogPath {
Param(

    [Parameter(Mandatory=$true,Position=0)]
    [ValidateSet("2010","2013","2016","2019")]
    [string]$EXVersion="2016"
)

$ExchangeLogPath = New-Object -TypeName psobject
Switch ($EXVersion) {

    {($_ -eq "2013") -or ($_ -eq "2016") -or ($_ -eq "2019")}  {
            $ExchangeLogPath | Add-Member -NotePropertyName "IISLogs" -NotePropertyValue "C:\inetpub\logs\LogFiles\"
            $ExchangeLogPath | Add-Member -NotePropertyName "ExchangeLogging" -NotePropertyValue "C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
            $ExchangeLogPath | Add-Member -NotePropertyName "ETLTracesLogs" -NotePropertyValue "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
            $ExchangeLogPath | Add-Member -NotePropertyName "DiagnosticsLogs" -NotePropertyValue "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs"
    }
    "2010" {
            $ExchangeLogPath | Add-Member -NotePropertyName "IISLogs" -NotePropertyValue "C:\inetpub\logs\LogFiles\"
            $ExchangeLogPath | Add-Member -NotePropertyName "ExchangeLogging" -NotePropertyValue "C:\Program Files\Microsoft\Exchange Server\V14\Logging\"
            $ExchangeLogPath | Add-Member -NotePropertyName "ETLTracesLogs" -NotePropertyValue "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Search\Ceres\Diagnostics\ETLTraces\"
            $ExchangeLogPath | Add-Member -NotePropertyName "DiagnosticsLogs" -NotePropertyValue "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Search\Ceres\Diagnostics\Logs"
    }
    default {
            $ExchangeLogPath | Add-Member -NotePropertyName "IISLogs" -NotePropertyValue "C:\inetpub\logs\LogFiles\"
            $ExchangeLogPath | Add-Member -NotePropertyName "ExchangeLogging" -NotePropertyValue "C:\Program Files\Microsoft\Exchange Server\V14\Logging\"
            $ExchangeLogPath | Add-Member -NotePropertyName "ETLTracesLogs" -NotePropertyValue "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Search\Ceres\Diagnostics\ETLTraces\"
            $ExchangeLogPath | Add-Member -NotePropertyName "DiagnosticsLogs" -NotePropertyValue "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Search\Ceres\Diagnostics\Logs"
    }

}
return $ExchangeLogPath
}