In line with the past two articles published we will examine another source of disk space usage on an Exchange or IIS servers. This seemingly innocuous source of space usage can creep up on Administrators who are unaware that Exchange Servers for example do log a lot of information to these IIS log files on busy servers. Depending on the configuration the server could create dozens or more files a day as well as fill an entire drive because the files are not truncated. Monitoring this can be a task that an administrator could automate and keep from being an issue.
In this article we will construct a script that will look at all IIS log files and keep x amount of days’ worth of logs on the server. By culling this at a fixed data range, an administrator will always have a set amount of logs to examine in case there are issues. If this timeframe is too short, than it can be extended as needed. Let’s dive right into the script.
Connecting to an Exchange Server
In this script we will assume that a jump or admin box is being used, which means that the Exchange PowerShell module may not be installed. As such, we will run this with regular Windows PowerShell and connect to a remote Exchange Server of our choice.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.domain.com/PowerShell/ -Authentication Kerberos Import-PSSession $Session -DisableNameChecking
Keep in mind that ‘exchange.domain.com’ is simply the FQDN (Fully Qualified Domain Name) of the Exchange Server with which we will run our cmdlets from.
Setup an Output File and Add A Header
$Date = Get-Date -Format "MM.dd.yyyy-hh.mm-tt" $OutputFile = 'C:\Temp\IISCleanupScript'+$Date+'.txt' $Output = "Server,Directory,FileCount,LastLogWrite,FilesRemoved" | Out-File $OutputFile
List of Exchange Databases
As this script will need to analyze each Exchange Server, we’ll need to pull that list. To make the list cleaner we will select just the name and then sort that list. All of this can be done with one line:
$Databases = Get-MailboxDatabase -Status
Checking Each Exchange Server’s IIS Settings
After getting a list, we can use a Foreach statement to kick off a loop for each database found:
Foreach ($ExchangeServer in $ExchangeServers) {
Pull a List of Log File Folders
IIS can have one or more folders that contain log files, with this one liner we can discover how many directories are present and store them in a variable for log discovery.
$Names = (Get-ChildItem \\$ExchangeServer\C$\inetpub\logs\LogFiles).Name
Review Each Directory Discovered
In most cases an Exchange Server will have two or more directories we need to examine. We can review each and process the files in those directories:
Foreach ($Name in $Names) {
Set Up Variable for Loop
In this case we read in the locations being examined as well as set some counters and variables to make this section run right, but will also clear out any values that may corrupt our discovery process.
$Location = "\\$ExchangeServer\C$\inetpub\logs\LogFiles\$Name" $Filesremoved = 0 $Count = 0 $LastWrite = 'N/A'
Count Files and Get Last Write Date
Try { $Count = (Get-ChildItem $Location -ErrorAction stop).Count If ($Count -gt 0) { $LastWrite = (Get-ChildItem $Location | Sort LastWriteTime | Select -Last 1).LastWriteTime } } Catch { $Count = 0 $LastWrite = 'N/A' }
Remove Files Older Than Fourteen Days
In this section any file over 14 days in age is removed and kept track of as we use the $FilesRemoved variable as a counter.
If ($Count -gt 0) { $Today = Get-Date $FourteenDays = ($Today).AddDays(-14) $Names = (Get-ChildItem $Location | Where {$_.LastWriteTime -gt $FourteenDays}).Name $Files = (Get-ChildItem $Location).Name Foreach ($File in $Files) { If ($Names -NotContains $File) { $FileToRemove = $Location+'\'+$File Remove-Item $FileToRemove $Filesremoved++ } } }
Output Each Server’s Results
After Each server’s files are processed, the results are exported to a Results file.
$Output = "$ExchangeServer,$Name,$Count,$LastWrite,$FilesRemoved" | Out-File $OutputFile -Append
Sort Results by Exchange Server
To organize the results, we can sort the output file, by the name of the Exchange Server and export the results to a new file.
$File = Import-CSV $OutputFile $File | Sort Server | ft > IISSortedReport.txt
Sample Report
Notice in the below report, each server has two directories in IIS that are analyzed:
Conclusion
While cleaning up log files is not an exciting topic for any administrator, it is an important topic that can sometimes bite us in the rear. As such, having a script in your pocket that you can use to cleanup extraneous log field can be useful. The script is simple and adjustable as we can change the window of log files from 14 days to whatever is needed to support the environment. We also have reporting so that an admin knows how many logs files are retained and how many were deleted from each server and each folder in the IIS folder.
In the next article in this series we will review general disk space review to keep up with general maintenance of servers.
Download the Script
IIS-LogCleanupScript.txt
**do not forget to change the extension to .PS1**