The PowerShell script to purge Exchange Server and IIS log files has been updated to version 2.1.
The function Copy-LogFiles has been slightly rewritten and there has been a change in the cmdlet parameters.
When using ArchiveMode CopyAndZip or CopyZipAndDelete all copied log files in the EXCHANGESERVER\LOGS folder are added to a compressed archive. The script creates a separate archive for IIS and Exchange logs.
Code updated
function Copy-LogFiles { [CmdletBinding()] param( [string]$SourceServer, [string]$SourcePath, $FilesToMove, [string]$ArchivePrefix = '' ) if($SourceServer -ne '') { # path per SERVER for zipped archives $ServerRepositoryPath = Join-Path -Path $RepositoryRootPath -ChildPath $SourceServer # subfolder used as target for copying source folders and files $ServerRepositoryLogsPath = Join-Path -Path $ServerRepositoryPath -ChildPath $LogSubfolderName $ServerRepositoryPath = Join-Path -Path $RepositoryRootPath -ChildPath $SourceServer if(!(Test-Path -Path $ServerRepositoryPath)) { # Create new target directory for server, if does not exist $null = New-Item -Path $ServerRepositoryPath -ItemType Directory -Force -Confirm:$false } foreach ($File in $FilesToMove) { # target directory $targetDir = $File.DirectoryName.Replace($TargetServerFolder, $ServerRepositoryLogsPath) # target file path $targetFile = $File.FullName.Replace($TargetServerFolder, $ServerRepositoryLogsPath) # create target directory, if not exists if(!(Test-Path -Path $targetDir)) {$null = mkdir -Path $targetDir} # copy file to target $null = Copy-Item -Path $File.FullName -Destination $targetFile -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue } if($ZipArchive) { # zip copied log files $Archive = Join-Path -Path $ServerRepositoryPath -ChildPath ('{0}-{1}' -f $ArchivePrefix, $ArchiveFileName) $logger.Write(('Zip copied files to {0}' -f $ArchiveFileName)) # delete archive file, if already exists if(Test-Path -Path $Archive) {Remove-Item -Path $Archive -Force -Confirm:$false} try { # create zipped asrchive Add-Type -AssemblyName 'System.IO.Compression.FileSystem' [IO.Compression.ZipFile]::CreateFromDirectory($ServerRepositoryLogsPath,$Archive) } catch { $logger.Write(('Error compressing files from {0} to {1}' -f $ServerRepositoryLogsPath, $Archive),3) } finally { # cleanup, if compression was successful if($DeleteZippedFiles) { $logger.Write(('Deleting folder {0}' -f $ServerRepositoryLogsPath)) $null = Remove-Item -Path $ServerRepositoryLogsPath -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue } } } } }
The PowerShell script to purge Exchange Server and IIS log files has been updated to version 2.0.
Release 2.0 allows for copying of files that will be deleted to be copied to a central file repository. The script will create a folder per server and the full log file folder structure will be preserved.
The next release will contain an option to compress the copied log files.
Added code:
function Copy-LogFiles { [CmdletBinding()] param( [string]$SourceServer, [string]$SourcePath, $FilesToMove ) if($SourceServer -ne '') { # path per SERVER for zipped archives $ServerRepositoryPath = Join-Path -Path $RepositoryRootPath -ChildPath $SourceServer # subfolder used as target for copying source folders and files $ServerRepositoryLogsPath = Join-Path -Path $ServerRepositoryPath -ChildPath $LogSubfolderName $ServerRepositoryPath = Join-Path -Path $RepositoryRootPath -ChildPath $SourceServer if(!(Test-Path -Path $ServerRepositoryPath)) { # Create new target directory for server, if does not exist $null = New-Item -Path $ServerRepositoryPath -ItemType Directory -Force -Confirm:$false } foreach ($File in $FilesToMove) { # target directory $targetDir = $File.DirectoryName.Replace($TargetServerFolder, $ServerRepositoryLogsPath) # target file path $targetFile = $File.FullName.Replace($TargetServerFolder, $ServerRepositoryLogsPath) # create target directory, if not exists if(!(Test-Path -Path $targetDir)) {$null = mkdir -Path $targetDir} # copy file to target $null = Copy-Item -Path $File.FullName -Destination $targetFile -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue }-Force if($ZipArchive) { # zip copied log files # <# NOT FULLY TESTED YET $Archive = Join-Path -Path $ServerRepositoryPath -ChildPath $ArchiveFileName $logger.Write(('Zip copied files to {0}' -f $ArchiveFileName)) if(Test-Path -Path $Archive) {Remove-Item $Archive -Force -Confirm:$false} Add-Type -AssemblyName 'System.IO.Compression.FileSystem' [IO.Compression.ZipFile]::CreateFromDirectory($ServerRepositoryLogsPath,$Archive) #> } } }
This script deletes all Exchange and IIS logs older than X days from all Exchange 2013+ servers that are fetched using the Get-ExchangeServer cmdlet.
The Exchange log file location is read from the environment variable and used to build an administrative UNC path for file deletions.
Optionally, you can use the Active Directory configuration partition to determine the Exchange install path dynamically, if supported in your Active Directory environment.
# EXAMPLE 1 # Delete Exchange and IIS log files older than 14 days .\Purge-LogFiles -DaysToKeep 14 # EXAMPLE 3 # Delete Exchange and IIS log files older than 7 days with automatic discovery .\Purge-LogFiles -DaysToKeep 7 -Auto # EXAMPLE 3 # Delete Exchange and IIS log files older than 7 days with automatic discovery and send email report .\Purge-LogFiles -DaysToKeep 7 -Auto -SendMail -MailFrom postmaster@sedna-inc.com -MailTo exchangeadmin@sedna-inc.com -MailServer mail.sedna-inc.com # EXAMPLE 4 # Delete Exchange and IIS log files older than 14 days, but copy files to a central repository and compress the log files before final deletion .\Purge-LogFiles -DaysToKeep 14 -RepositoryRootPath \\OTHERSERVER\OtherShare\LOGS -ArchiveMode CopyZipAndDelete # EXAMPLE 5 # Delete Exchange Server, IIS, and HTTPERR log files older than 7 days, and send an HTML email. Identify Exchange file paths using AD configuration objects. .\Purge-LogFiles.ps1 -DaysToKeep 7 -SendMail -MailFrom postmaster@sedna-inc.com -MailTo exchangeadmin@sedna-inc.com -MailServer mail.sedna-inc.com -UseDynamicExchangePaths -IncludeHttpErr
Brian Reid, C7 Solutions, http://www.c7solutions.com/2013/04/removing-old-exchange-2013-log-files-html
This script reconfigures the IIS log folder to use a different folder instead of the default C:\inetpub\logs folder.
Additionally, the log settings can be adjusted as well. The script changes the default log file location and settings on a server level.
By default, the settings are inherited by websites. If manual changes have been made on a website level, not all settings will be inherited.
Change the IIS log file location to D:\IISLogs
.\Set-Webserver.ps1 -LogFolderPath D:\IISLogs
Change the IIS log period to an hourly period
.\Set-Webserver.ps1 -LogFilePeriod Hourly
Use the local time for filenames and log file rollover
.\Set-Webserver.ps1 -LocalTimeRollover $true
Additional credits go to Michel de Rooij, https://eightwone.com