PSG2013: Beginner Event 1: Managing Old Log Files

This is my submission for Event 1 in the beginner track for the PowerShell Scripting Games, 2013

Here’s the instructions. The premise of the event is log files have been allowed to accumulate and are now eating disk space on a server. Files accumulate in C:\Application\Log\[Application Name]. We need to identify and move files that are older than 90 days to an archive located at \\NASServer\Archive. When moving, the files should retrain the path with the application name in it. This means C:\Application\Log\App1\OldLogFile.log should land as \\NASServer\Archive\App1\OldLogFile.log. As concise as possible, One-Liners are appreciated.

Here’s my submission:

#Broken Up
Get-ChildItem C:\Application\Log\*\* | 
ForEach { 
if (((Get-Date)-$_.lastaccesstime).TotalDays -gt 90) { 
   Move-Item $_ \\NASServer\Archive\$(($\$($ 
#Retained on one line
Get-ChildItem C:\Application\Log\*\* | ForEach { if (((Get-Date)-$_.lastaccesstime).TotalDays -gt 90) { Move-Item $_ \\NASServer\Archive\$(($\$($ } }

Managing Old Log Files

So first things first, we need to actually go out and get all the log files. This turned out to be amazingly simple.

Get-ChildItem C:\Application\Log\*\*

I didn’t realize Get-ChildItem (which is what the dir alias is in PowerShell) would let me double wildcard like this. I found it just playing around with the cmdlet. But once I have the return, that is and object for every file from every folder beneath Application\Log, I can start examining them directly to make a decision and do something.

From there I need to identify which files are older than 90 days and take an action. The LastAccessTime Property of a filesystem object contains a standard date object, which I can simply subtract from the date right now (Get-Date). This results in a timespan object that has a non-incremental TotalDays property.

ForEach { if (((Get-Date) - $_.lastaccesstime).TotalDays -gt 90) { #Do Something } }

From here, its just a matter of moving files. To pick the object to move, I simply specify the object on the pipeline $_. To retain the folder structure, I had to get a little more creative. Each filesystem object has a directory property, which is a call for the directory that houses the file. By calling this and looking at its name, I can specify it in the destination path for Move-Item. Simply specify the name of the file after we’ve modified the path. I need the extra dollar sign to signify to PowerShell that this string should be the result of enumerating that property. $($ is a variable stand-in for the name of the file i.e. Someoldlogfile.log

Move-Item $_ \\NASServer\Archive\$(($\$($

Simply string it all together. No Output if it succeeds, errors are not suppressed. Move-Item will complain if the destination path does not exist, but on an archive server it stands to reason the path probably already exists, or that I’d at least be willing to create it the first time I tried to do this task.

The advanced version calls for parameterizing the paths to examine as well as the time-frame to judge on. That doesn’t sound terribly difficult, so I will look at it next.

Jeff is a system administrator for an enterprise of over 4000 devices. He is comfortable on Windows and *nix and participates in everything from desktop to network support. In addition, he develops in PowerShell to aid in the automation of administrative tasks.

When not at work, Jeff is adjunct faculty at the University of Alaska Anchorage, where he teaches PC Architecture to first year students in the Computer and Networking Technology Program. He is also working on new curriculum in PowerShell and Virtualization as well as leading department technology deployments.

View Jeff Liford's profile on LinkedIn

Posted in Uncategorized