PowerShell Potluck December 2024
You've done it. You've reached the end of another month and for that matter, an entire year of hopefully interesting and useful PowerShell content. I'll keep this short so that you can enjoy the end-of-the-year festivities with your family and friends.
PowerShell+DevOps Global Summit 2025
Tickets are on-sale now for next year's PowerShell+DevOps Global Summit in Bellevue, WA. Next year's dates are April 7-10. This is the premier PowerShell event in North America. If PowerShell is a critical part of your day job, this is the event you don't want to miss.
This is three days of jammed-packed and intense PowerShell content that you won't find anywhere else. The event size is intentionally limited to keep the experience intimate and to allow you to network with the speakers and other attendees. Many first time attendees are amazed at the community and the event, especially the evening "bar" sessions. There is so much more to this event than the sessions. Ask anyone who has attended before.
Next year's event will include rare appearances by Don Jones and Jeffrey Snover. This will be one the even rarer times, that Don and I are at the same event. If you have any of our books you'd like signed, this may be one of the last opportunities to get that done.
I encourage you to checkout the schedule and get your tickets soon. I've been told that early bird pricing ends on January 10.
OnRamp
Associated with the PowerShell Summit is OnRamp. This is a conference-within-a-conference that I have been involved with for several years. This event is targeted for entry-level IT professionals, students, and those who are new to PowerShell. This is a great opportunity to get a jump start an automation or DevOps career. The idea is that the OnRamp content provides a foundation that will help you get more out of the PowerShell Summit content when you come back the following year.
There are a limited number of scholarships available for OnRamp. The scholarship provides a full ticket, hotel, and domestic airfare. This is a terrific way for someone just getting started in their career to attend the event and jump start their career. If you know someone who would benefit from this, please encourage them to apply.
You can learn more about OnRamp and the scholarship program at https://www.powershellsummit.org/onramp/.
Compressing JSON
Here's a PowerShell tip you might find useful. Did you know that you can compress JSON? Most of the time, we're using the ConvertTo-Json
cmdlet like this:
PS C:\> Get-Service bits | Select-Object -ExcludeProperty *services* | ConvertTo-Json
{
"UserName": "LocalSystem",
"Description": "Transfers files in the background using idle network bandwidth. If the service is disabled, then any applications that depend on BITS, such as Windows Update or MSN Explorer, will be unable to automatically download programs and other information.",
"DelayedAutoStart": true,
"BinaryPathName": "C:\\WINDOWS\\System32\\svchost.exe -k netsvcs -p",
"StartupType": 10,
"Name": "bits",
"CanPauseAndContinue": false,
"CanShutdown": false,
"CanStop": true,
"DisplayName": "Background Intelligent Transfer Service",
"MachineName": ".",
"ServiceName": "bits",
"StartType": 2,
"ServiceHandle": {
"IsInvalid": false,
"IsClosed": false
},
"Status": 4,
"ServiceType": 48,
"Site": null,
"Container": null
}
In Windows PowerShell, you'll need to specify the properties more directly with Select-Object
.
Get-Service bits | Select-Object -ExcludeProperty *services* -property * | ConvertTo-Json
The output is much easier to read. If you save the JSON to a file it is much easier to edit. But if you don't need that, you can compress the JSON like this:
PS C:\> Get-Service bits | Select-Object -ExcludeProperty *services* | ConvertTo-Json -Compress
{"UserName":"LocalSystem","Description":"Transfers files in the background using idle network bandwidth. If the service is disabled, then any applications that depend on BITS, such as Windows Update or MSN Explorer, will be unable to automatically download programs and other information.","DelayedAutoStart":true,"BinaryPathName":"C:\\WINDOWS\\System32\\svchost.exe -k netsvcs -p","StartupType":10,"Name":"bits","CanPauseAndContinue":false,"CanShutdown":false,"CanStop":true,"DisplayName":"Background Intelligent Transfer Service","MachineName":".","ServiceName":"bits","StartType":2,"ServiceHandle":{"IsInvalid":false,"IsClosed":false},"Status":4,"ServiceType":48,"Site":null,"Container":null}
There are advantages to this. Let me save all service information to JSON files.
Get-Service | Select-Object -ExcludeProperty *services* | ConvertTo-Json | Out-File c:\temp\svc.json
Get-Service | Select-Object -ExcludeProperty *services* | ConvertTo-Json -compress | Out-File c:\temp\svc2.json
The first command took 443ms and the compressed file took 405ms. Naturally, the compressed version is smaller.
PS C:\> dir c:\temp\svc*.json | Select-Object Name,Length
Name Length
---- ------
svc.json 258929
svc2.json 207480
The smaller size makes a difference when bringing the data back into PowerShell.
PS C:\> (Measure-Command { Get-Content c:\temp\svc.json | ConvertFrom-Json}).ToString()
00:00:00.0690757
PS C:\> (Measure-Command { Get-Content c:\temp\svc2.json | ConvertFrom-Json}).ToString()
00:00:00.0080775
That is a significant difference. If you are creating JSON files for storage or transmission only, consider using the -Compress
parameter in your code.
SkyLine
This month I introduced you to gh.exe
extensions. Since we are at the end of the year, you might want to try the Skyline extension. The extension will present a visualization of your GitHub activity for the past year and create a binary STL file that you can use to 3D print a skyline of your GitHub activity.
PS C:\> gh skyline -h
GitHub Skyline creates 3D printable STL files from GitHub contribution data.
It can generate models for specific years or year ranges for the authenticated user or an optional specified user.
While the STL file is being generated, an ASCII preview will be displayed in the terminal.
ASCII Preview Legend:
' ' Empty/Sky - No contributions
'.' Future dates - What contributions could you make?
'░' Low level - Light contribution activity
'▒' Medium level - Moderate contribution activity
'▓' High level - Heavy contribution activity
'╻┃╽' Top level - Last block with contributions in the week (Low, Medium, High)
Layout:
Each column represents one week. Days within each week are reordered vertically
to create a "building" effect, with empty spaces (no contributions) at the top.
Usage:
skyline [flags]
Flags:
-d, --debug Enable debug logging
-f, --full Generate contribution graph from join year to current year
-h, --help help for skyline
-o, --output string Output file path (optional)
-u, --user string GitHub username (optional, defaults to authenticated user)
-w, --web Open GitHub profile (authenticated or specified user).
-y, --year string Year or year range (e.g., 2024 or 2014-2024) (default "2024")
If you don't specify an output path, the STL file will be created in the current directory.
PS C:\> gh skyline -y 2024 -o c:\temp\jdhitsolutions2024.stl
____ _ _ _ _ _
/ ___(_) |_| | | |_ _| |__
| | _| | __| |_| | | | | '_ \
| |_| | | |_| _ | |_| | |_) |
\____|_|\__|_| |_|\__,_|_.__/
____ _ _ _
/ ___|| | ___ _| (_)_ __ ___
\___ \| |/ / | | | | | '_ \ / _ \
___) | <| |_| | | | | | | __/
|____/|_|\_\\__, |_|_|_| |_|\___|
|___/
..
╻ ╻ ┃ ╻ .
░ ░ ╻ ░ ┃ ░ .
╻░╻░ ░ ╻╻╻ ╻▒ ┃ ╻ ▓╻ ░ ╻ .
╻ ░░░░ ╻ ░╻░▓▒ ░▓ ╻ ░╻ ╻ ╻ ░┃ ╽░░ ░ ░ ..
░╻ ╻░░░░ ╻░ ╻░▓▓░░╻░░╻░ ╻ ╻░░ ▒╻ ░ ░░╻▒░░╻░┃ ░╻ ..
░░░░░▓▓░▒░░░░░░░░░░░░░░ ░ ░░░░░░▒ ░▓░░▒░░░▒░▓░ ░░░..
jdhitsolutions
2024
Model generation complete: 341160 total triangles
STL file written successfully to: c:\temp\jdhitsolutions2024.stl
If you don't have a 3D printer, you can upload the file to GitHub and see it rendered. Checkout my GitHub 2024 skyline. I also created a Skyline for 2019-2024
Or use a site like viewstl.com
Now, I have a reason to get a 3D printer.
A New Scripting Challenge
Before we wrap things up, let me leave you with a new scripting challenge, and this will be a tough one. But as they say, "No pain, No gain." Write a PowerShell function or script that will create a JSON comment block for a .ps1 script file that looks like this:
{
"Date": "2024-12-17T17:19:22.1426831-05:00",
"Path": "C:\\scripts\\test-powershellrun.ps1",
"RequiredVersion": "7.4",
"RequiresAdmin": false,
"Commands": [
"Find-PSResource",
"Get-Module"
],
"Tags": [
"PowerShellRun",
"Profile"
]
}
The Commands
property should be a unique list of cmdlets and functions used in the script or function. For bonus points, exclude common cmdlets like Select-Object
, Where-Object
, etc.
The easy version of this challenge is to create a PowerShell solution that creates a single JSON file for all files processed. Your solution should take advantage of the PowerShell pipeline. The real challenge is to append the JSON block as a multi-line comment to the end of the script file. You can ignore files that contain multiple functions. You will need to create a PowerShell set of tools to update the JSON data and retrieve the JSON data, converting it to a PowerShell object.
Overachievers will find a way to insert the JSON block into function definitions within the script file. This should allow you to process .ps1 files with multiple functions. If you tackle this, you'll want to extend the JSON to include the function name.
Tips - You can use the AST to parse the script file and find the commands used. - I expect you will need to manually define tags for each file or group of files. - If using regular expressions, investigate multi-line pattern matching. - Only process .ps1 files.
Summary
I want to end by thanking all of you for your support this year, especially my premium subscribers. You support makes it possible for me to devote time to this project. If you are not a premium subscriber, I hope you'll think about becoming one in 2025.
🍾 Hoping you all have a healthy and happy New Year.