Sunday, December 20, 2015

Using Task Scheduler to Purge Microsoft Exchange and IIS Logs

Pingback from Shane Jackson blog

Microsoft Exchange creates a lot of logs. Unless these are managed, they can quickly fill up your disk space. The following describes one way to use PowerShell and Task Scheduler to automatically purge the Exchange 2013+ and IIS logs. Scheduled task would be useful for Hybrid deployments where Exchange Server is used just for managing mailbox properties (applies Hybrid Identities) and you are not really too much interested in deeper diagnostic logs.

Scheduled Task Summary
Task NamePurge Exchange logs older than 7 days
FunctionDeletes all Exchange logs older than 7 days from the following location ‘c:\program files\microsoft\exchange server\V15\Logging’
ScheduleDaily at 1am
Program CalledC:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Parameters-NonInteractive -WindowStyle Hidden -command “. ‘c:\program files\microsoft\exchange server\V15\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; gci ‘c:\program files\microsoft\exchange server\V15\Logging’ -Directory | gci -Include ‘*.log’,’*.blg’ -Recurse | ? LastWriteTime -lt (Get-Date).AddDays(-7) | Remove-Item
Runs AsSYSTEM
Task NamePurge IIS logs older than  14 days
FunctionDeletes all IIS logs older than 14 days from the following location ‘c:\inetpub\logs’
ScheduleDaily at 1am
Program CalledC:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Parameters-NonInteractive -WindowStyle Hidden -command “. ‘c:\program files\microsoft\exchange server\V15\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; gci ‘C:\inetpub\logs’ -Directory | gci -Include ‘*.log’,’*.blg’ -Recurse | ? LastWriteTime -lt (Get-Date).AddDays(-14) | Remove-Item
Runs AsSYSTEM