My Solution to Iron Scripter 2019 Preparatory Challenge 1

Anyone who has competed in the scripting games before knows that I’m always looking for a challenge when it comes to writing PowerShell code. While the scripting games haven’t been held in the last several years, they’ve somewhat been replaced by the Iron Scripter competition at the PowerShell + DevOps Global Summit and 2019 is shaping up to be no different. Think you’ve got skills? Bring them on! and Get-Involved.

Whether you’ve previously competed or not, you should definitely head over to IronScripter.us to see what it’s all about. I’m not sure if there will be more than one practice scenario or not since I’m in no way officially affiliated with the competition other than being a participant. This particular scenario deals with the Get-Counter cmdlet which I used in my chapter on Finding Performance Bottlenecks with PowerShell in The PowerShell Conference Book (you can read my chapter in its entirety by downloading the free sample).

The challenge is to create a PowerShell function that Get-Counter can be piped to which produces friendlier output and returns specific properties. I used a regular expression in my chapter from the previously referenced book. I simply reused it for this portion of the challenge.

I try to avoid the ComputerName parameter of commands when targeting remote systems because they’re usually implemented via older DCOM protocols which are typically blocked by firewalls on modern systems. I simply use Invoke-Command to run commands locally on remote systems via PowerShell remoting since WinRM is much more firewall friendly. Because of this design philosophy, I never tested my regular expressions when targeting remote systems with Get-Counter‘s ComputerName parameter. Well, they don’t work because of an extra slash that exists after the computer name in the results when targeting a remote system.

While I could rework the regex to take that into account, I’ve been there and done that so I thought I would take a different approach and eliminate the regular expressions altogether. While I’m at it, I’ll also tackle the advanced portion of the challenge which wants a custom type and view defined to return four specific properties by default in a table.

I’m writing my code in a way that requires PowerShell version 3.0 or higher and since Get-Counter only exists in Windows PowerShell, I’ll define both of those requirements in a requires statement. I’m using an approved verb along with a singular noun and the command name is in Pascal case. This is followed by comment based help. CmdletBinding is declared which makes it an advanced function, the output type is declared, and then the single InputObject parameter which only accepts the type of object that Get-Counter produces. The parameter is mandatory and accepts input via the pipeline by value (what I call by type).

I’m splitting the Path property on backslashes, but since there are some double backslashes, I’m using the Split method instead of the Split operator so I can take advantage of the .NET option to remove empty entries. A For loop is used to iterate through the entries. PSCustomObject is used to create a custom object and a type name is specified within the PSCustomObject to define a custom object type. Finally, since only four properties are required in the default output and four or fewer properties are displayed in a table by default, a Default Display Property Set is defined instead of going through the trouble of creating a module, manifest, and custom format file in XML.

By default, the output is the four properties required by the advanced challenge specifications. All of the properties can always be viewed in either a list (or table) when piping to either Format-List, Format-Table, or Select-Object and specifying the asterisk wildcard (*) as the value for the Property parameter.

The Format-MrCounter function shown in this blog article can be downloaded from my IronScripter repository on GitHub. I’d love to hear your thoughts. Please post your questions, comments, and/or suggestions as a comment to this blog article.

If you enjoyed this blog article and the techniques I used in my solution, then I hope to see you in my Finding Performance Bottlenecks with PowerShell session at the PowerShell + DevOps Global Summit 2019.

µ

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: