
One of PowerShell’s greatest strengths is its ease of use and flexibility. There are many things you do in PowerShell that could be a whole lot more complicated. For example, you don’t have to learn different commands for navigating the registry than what you use for the file system. The same command, Get-ChildItem, can work in both. True, you have to learn a few things depending on where you are running the command. This is also true for commands like Select-Object and Measure-Object. They are generic and flexible enough to use with any type of object.
Get-ObjectAge
For your next challenge the Chairman would like you to create an advanced function called Get-ObjectAge. Many things that you manage with PowerShell have an age. Think of files, processes, Active Directory objects, and more. These objects often have properties that indicate when the object was created and when it was last modified or updated. Obviously, the property names vary.
Your task is to create a generic function that can be used with any group of objects as input. The output should show the object’s creation time, when it was last modified, and its age from creation. You also need to define a property that indicates if the creation age is above or below, the average of all processed objects. Finally, it would be nice to allow the user to specify additional object properties to display.
You can assume the user of your function will be able to specify the property names to use. Although, if you want to build some mechanism to autodetect the correct properties, The Chairman won’t stand in your way.
Bonus
Given the generic nature of the command, formatted output might be tricky. But not impossible if you limit your focus. See if you can create the function and other necessary files to give you nicely formatted results for common file types like process, file, and Active Directory User account. If you tackle this bonus, format timespans without the millisecond component.
You might also consider using ANSI formatting to display Above average objects in a different color.
This is my second participation to an Iron Scripter Challenge.
Once again it’s been interesting to work on an ‘abstract’ problem you wouldn’t have thought upon. And again I had a lot of learnings while creating my solution.
– Where to place a local function to not expose it to the wild (usually I work on modules where you just don’t export them) when working with pipeline
– generic functions are harder to design
– abstract problems can bring new ideas to you to be used in your every days life
Sorry, that I missed the bonus, but a awaiting new learnings from others solutions.
If you want to have a look in my repo:
https://github.com/danubie/Ironscripter/blob/master/ObjectAge/README.md
Thanks for your effort in creating puzzles.
Glad to see you take to the challenges and realize the process is more important than the end result.
Here’s the first part of my solution. https://jdhitsolutions.com/blog/powershell/7537/solving-the-powershell-object-age-challenge-part-1/
Here’s another, more advanced solution. https://jdhitsolutions.com/blog/powershell/7543/solving-the-powershell-object-age-challenge-part-2/