PowerShell 10 Year Anniversary Code Golf Winners
source link: https://thomasrayner.ca/powershell-10-year-anniversary-code-golf-winners/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
PowerShell 10 Year Anniversary Code Golf Winners
For the PowerShell 10 Year Anniversary, Will Anderson (@GamerLivingWill on Twitter) and I (@MrThomasRayner on Twitter) ran a three-hole code golf competition on code-golf.com, a site developed by fellow MVP Adam Driscoll.
Here is the link to all the background info on the competition: https://github.com/ThmsRynr/PS10YearCodeGolf . Check this page out for links to the individual holes, too.
So, without further delay, let’s announce the winners!
Hole 1
The challenge was to get all the security updates installed on the local computer in the last 30 days and return the results in the form of a [Microsoft.Management.Infrastructure.CimInstance] object (or an array of them).
The winner of this hole is Simon Wåhlin. Here is their 46 character submission.
gcim(gcls *ix*|% *mC*e)|? I*n -gt((date)+-30d)
gcls *ix* gets the CimClass win32_quickfixengineering and % *mC*e gets the CimClassName property. gcim is an alias for Get-CimInstance which, as per the previous section, is getting the win32_quickfixengineering class. The results are piped into the where-object cmdlet where the property matching the pattern I*n (which happens to be InstalledOn) is greater than the current date, minus 30 days.
Hole 2
The challenge was to get the top ten file extensions in c:\windows\system32, only return 10 items and group results by extension.
The winner of this hole is Simon Wåhlin again. Here is their 42 character submission.
(ls C:\*\s*2\*.*|% E*n|group|sort c* -d)[0..9]
ls c:\*\s*2\*.* means Get-ChildItem where the path is c:<em><any directory>\<a directory matching s*2>\<files, not directories> </em>and this pattern only matches the path c:\windows\system32\<files>. This is piped into the foreach-object cmdlet to retrieve the property that matches the pattern E*n,which is the Extension property. The extensions are piped into the sort-object cmdlet, sorted by the property that matches the pattern c*, which is count, and returned in descending order. This is an array, and the items in positions 0-9 are returned.
There were shorter submissions for this hole that didn’t explicitly target c:\windows\system32 and therefore missed the challenge. You could not assume we were already on c: or running as admin, etc. Some solutions included folders in the results which also missed the challenge.
Hole 3
The challenge was to get all the active aliases that are fewer than three characters long and do not resolve to a Get- command. For this hole, even though it wasn’t in the Pester test, you had to assume that non-standard aliases might be on the system. That’s why we specifically mentioned that we didn’t want you to return aliases that resolve to Get-*, and the Pester test checked the ResolvedCommand.Name property of the aliases you returned.
To break some submissions that didn’t check what the aliases resolved to, you could just run New-Alias x Get-ChildItem to create a new alias of ‘x’ that resolves to Get-ChildItem.
The winner of this hole is EdijsPerkums. Here is their 24 character submission.
gal ?,??|? Di* -Notm Get
Get-Alias is passed an array of regex patterns, ?,?? which corresponds to one and two characters. The results are piped into the where-object cmdlet to isolate aliases whose property that matches the pattern Di* (DisplayName) doesn’t match Get.
Congratulations to all the winners! We will be in touch to get you your prizes. We hope you all had fun with this mini-competition. Don’t forget to check out all the terrific material from the PowerShell 10 Year Anniversary on Channel 9!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK