2013 PowerShell Scripting Games Advanced Event 1 – Parameters Don’t Always Work As Expected

The scenario for this event states the following which has been paraphrased:

Someone allowed log files to build up for around two years and they’re starting to causing disk space issues on the server. You need to build a tool to archive the log files because this has happened more than once. Applications write log files to a sub-folder in “C:ApplicationLog”. Examples are C:ApplicationLogApp1, C:ApplicationLogOtherApp, and C:ApplicationLogThisAppAlso. The log files have random file names and a .log extension. After the log files are written to disk, they’re no longer used by the applications.

Your tool should archive log files older than 90 days and move them to “\NASServerArchives, maintaining the source sub-folder stucture such as “\NASServerArchivesApp1”.

The source location,  destination location, and file age to archive should each be a parameter. Any errors that happen during the move should be clearly displayed. Output should not be displayed if the command completes successfully.

Using Get-ChildItem with the -Filter parameter:

I started out working with Get-ChildItem and was almost set on using the -Filter parameter to filter the results down to only log files, the problem is that using -Filter in that manner would also return any file extension that begins with log such as .log1 as shown in the following results:

sg-event1

So unfortunately if you used the -Filter parameter with Get-ChildItem in your solution, it doesn’t meets the requirements of this scenario since it will archive more than just .log files.

Using Get-ChildItem with the -Include parameter:

The other parameter that I thought would accomplish the task was Get-ChildItem with the -Include parameter. The issue I ran into with it is that it only works when using the -Recurse parameter. At first, using the -Recurse parameter didn’t seem to be an issue:

sg-event1a

After re-reading the scenario, I decided that I only wanted to move log files in the first level sub-folders and not in the actual “C:ApplicationLog” folder and not in sub-folders deeper than the first level sub-folders so as you can see in the previous image, the -Include parameter doesn’t accomplish that task.

I ended up using something similar to the following which retrieves only the files in the first level sub-folders that are older than 90 days without using either the -Filter or -Include parameter:

sg-event1b

I’ll continue this blog post tomorrow to discuss how I dynamically retrieved this information since the scenario states the folder names could change. I’ll discuss how I determined the folder names without making an additional call to the file system. I’ll also show you how I used the list of folder names instead of iterating through each file individually to move the files to the correct sub-folder on the destination end.

Want to see my solution? Click this link: Advanced 1: An Archival Atrocity: http://scriptinggames.org/entrylist.php?entryid=279. You’ll have to be signed in to see the solution. If you like my script, please vote for it!

Update 02/09/14
The link to my solution is no longer valid so I’m posting it here since I’ve received some requests for it:

This PowerShell script can also be downloaded from the TechNet script repository.

µ

2 Comments

  1. mjolinor

    I agree there is a potential for it to move files you didn’t intend to move if you specify the wrong thing for the filter, but I’m not sure that constitutes not meeting the requirements of the event.

    While the -Filter parameter does behave differently than the PS wildcarding for the Path parameter, as long as you specify in the parameter help that this is a filesystem provider filter, and not a wildcard-allowed PS path specification I’d think you should be good.

    Reply

Leave a Reply

%d bloggers like this: