Author: Brian Reid, Brianreidc7 (Twitter);

Total Page:16

File Type:pdf, Size:1020Kb

Author: Brian Reid, Brianreidc7 (Twitter);

##### # # Name: Remove-ExchangeLogFiles.ps1 # Author: Brian Reid, @BrianReidC7 (Twitter); http://www.c7solutions.com/ # Reference: http://www.c7solutions.com/2013/04/removing-old-exchange-2013-log-files-html # # This script will get all Exchange 2013 servers in the organization and then delete 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

$Days = 30 ### You can change the number of days here ### $ExchangeInstallRoot = "C" $IISLogPath = "Inetpub\logs\LogFiles\" $ExchangeLoggingPath = "Program Files\Microsoft\Exchange Server\V15\Logging\"

Write-Host "Removing IIS and Exchange logs; keeping last" $Days "days"

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 comment mark (#) from the front of this line to see files being deleted during script run ### 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} foreach ($E15Server In $EX2013) { CleanLogfiles($IISLogPath) CleanLogfiles($ExchangeLoggingPath) }

Recommended publications