Dealing with log files

I’m sure every administrator have to deal with some application or IIS filling up disk space with log files that are rarely needed but need to be kept for some reason. Let’s see a way of dealing with these logs and keep them under control.

This is the complete script, following the script you’ll find it again commented part by part. It is going to zip all the log files contained in some specific folders, and delete those zip files older tan x days.

Let’s now comment it by parts. First of all, I like to define all the variables I’m going to need so I don’t hardcode anything susceptible of being changed in the future.

  • $7z: I used 7-Zip because it’s free, but you can change the command line to adapt it to your favourite compressor.
  • $FileExtension: Extension of the files that we are going to compress and purge.
  • $DaysOld: Number of days that we’re going to keep old files. It’s a negative number because the DateTime object supports multiple methods to add days, hours, etc, but none to substract.

We’re also going to create an array with the paths where we’re going to process the log files by creating an array of custom PSObjects with the targets we want to work with.

  • $Targets is an array that we initialize empty and we’re going to fill up with as many PSObjects as we need.
  • The -Property parameter of a new PSObject has to be a hashtable. I created the hashtables with 2 keys, an ArchiveTargetName that we’ll use later for naming the zip files and an ArchiveTargetFolder for the path of the log files.

And some more variables we’re going to use

  • I don’t think I have to explain what $CurrentDate and $ComputerName contains ;)
  • And we’re going to use the GetInvalidFileNameChars method of System.IO to check the target names we defined earlier and make sure we get a zip file name we can actually use.

There is no reason for our script to try to compress log files if the 7-Zip executable is not where it’s supposed to be, right? And why let it throw an error when we can control it and cleanly stop the script?

Everything is ready and checked!! Let’s get this script to work by looping through all the targets we have defined.

We’ll replace invalid characters on the target name

And compress every file that matches our previously defined extension and hasn’t been modified since the day before (if you run the script in the AM after the logs have rotated, you’ll want to compress every log file except the one that is currently in use). Every file will be compressed into it’s own zip file and then removed.

All that’s left is delete the zip files older than $DaysOld

I hope it will be useful for you!!

The easy way of sending emails

A while ago I was looking for the way to send a mail at the end of a PowerShell script. There are hundreds of examples on the internet, but I don’t know why, most of them creating a SmtpClient object from .Net. Something like this

I’ve taken this example from a MSDN blog, it doesn’t release the 2 .Net objects it has created. It doesn’t really matter if you are going to send one mail and close the powershell.exe process, the objects will be released then. But you should be careful if you’re using it to send a lot of emails and/or on a production environment.

The Send-MailMessage cmdlet has been available since PowerShell 2.0! Isn’t it much cleaner and simple to do it like this?

In the TechNet link of the cmdlet you have all the available parameters in case you need to use credentials against your SMTP server, send to multiple recipients, etc.