Skip to content

Instantly share code, notes, and snippets.

View Jaykul's full-sized avatar
😀
Learning

Joel Bennett Jaykul

😀
Learning
View GitHub Profile
@Jaykul
Jaykul / About joining strings.md
Last active June 28, 2022 02:07
StringBuilder vs += vs -join @()

TL;DR: Use StringBuilder.

The truth: unless you're joining large amounts of long strings, the time it'll take PowerShell to read, parse, and compile the file is going to outweigh any improvements you make in the runtime unless you run the code repeatedly. This is easy to forget about, but try it for yourself. Download and run Strings.ps1 below, and then, run it a second time -- remember, you incur that first run penalty in each new PowerShell session.

image

For dozens to hundreds of strings, StringBuilder is only microseconds faster than +=

Obviously the results vary depending on your strings! The more there are, and the longer they are, the bigger gain you get from using StringBuilder. To sum up: StringBuilder is faster except in very small test cases, but it's not much faster except in extremely large test cases.

@Jaykul
Jaykul / About_Format-Wrap.md
Last active May 29, 2022 03:40
The most items, in the smallest space. And it works with strings.

Some experiments to see if we can make Format-Wide more dense. I made it handle strings, first...

And then built in colors, so it can show long lines wrapped. It just alternates colors and supports adding a little -Padding. By default there's one space between strings, but you can set it to 0, or to 2 or whatever you like.

If you pass one or more colors to the -RgbColor parameter like 0x336699, it automatically alternates colors and wraps lines, instead of doing columns -- unless you explicitly specify -Column or -AutoWrap.

Currently it's not totally compatible with the built-in Format-Wide, but it should seem compatible.

image

@Jaykul
Jaykul / Get-AcrTag.ps1
Created April 1, 2022 20:18
Search for repositories and get the latest tags
using namespace System.Collections.Generic
using namespace Microsoft.Azure.Commands.ContainerRegistry.Models
filter Get-AcrTag {
[Alias("Get-BicepTag","gat","gbt")]
[CmdletBinding()]
param(
# The (partial) name of the repository.
[Parameter(Mandatory, ValueFromRemainingArguments, Position = 0)]
[Alias("RepositoryName")]
@Jaykul
Jaykul / WslHelper.psm1
Last active January 28, 2024 22:54
Some functions I wrote to fix WSL problems
function ConvertFrom-IniContent {
<#
.SYNOPSIS
Parses content from ini/conf files into nested hashtables.
.EXAMPLE
Get-Content \\wsl$\Ubuntu\etc\wsl.conf | ConvertFrom-IniContent
.EXAMPLE
ConvertFrom-IniContent (Get-Content ~\.wslconf -Raw)
.EXAMPLE
"
@Jaykul
Jaykul / Select-CapturedString.ps1
Last active February 20, 2022 17:43
Collect named capture groups to allow parsing text even with uneven properties
function Select-CapturedString {
<#
.Synopsis
Collect named capture groups from regular expression matches
.Description
Takes string data and a regular expression containing named captures,
and outputs all of the resulting captures in one (or more) hashtable(s)
.Example
netstat | Select-CapturedString "(?<Protocol>\w{3})\s+(?<LocalIP>(\d{1,3}\.){3}\d{1,3}):(?<LocalPort>\d+)\s+(?<ForeignIP>.*):(?<ForeignPort>\d+)\s+(?<State>\w+)?"
@Jaykul
Jaykul / Add Attributes On Members.md
Last active March 27, 2022 00:36
Adding Attributes to Get-Member

I found an easy way to surface the attributes that are on properties of objects. If you update the MemberDefinition type, you can expose them so they show up when you run Get-Member.

@Jaykul
Jaykul / A day in the life of DevOps.md
Last active February 11, 2022 05:58
A day in the life of DevOps

My days are, perhaps, not typical.

As the Principal DevOps Engineer at my company, I spend an awful lot of time on Teams calls unblocking other engineers, or meeting with Platform, Operations, Architecture, or InfoSec to plan the next steps for Kubernetes and Azure, how we're going to do app configuration, etc.

My team has a backlog of automation projects, and 2-week sprints. We have a daily agile "stand up" meeting to touch base. Hypothetically, it's at the start of the day, but that will depend on your timezone -- for me it's after noon.

Projects usually take a few days to complete, but it frequently happens by spending a few hours on one thing, handing it off (to developers), and then starting something else while you wait for developer feedback. Sometimes you'll have a long running project where you'll work on the same thing for weeks. Some projects are short, 2-3 hour, projects like "on-boarding" new repositories or infrastructure configuration following well-established patterns. Others are research

@Jaykul
Jaykul / FrontDoor.bicep
Last active August 16, 2022 10:52
Azure Front Door with Rules Engine
@description('The name for the FrontDoor. Must be globally unique, one of a kind!')
param frontDoorName string
@description('This needs to be TRUE for the first deployment, but ONLY the first deployment')
param FirstTimeCleanDeployment bool = false
@description('A domain name that serves as the source of content for this FrontDoor')
param sourceDomain string = 'huddledmasses.org'
@description('Additional domains that will use javascripts from this CDN and must be added to the CORS rule')
@Jaykul
Jaykul / About.md
Last active March 27, 2022 00:40
Indent and Outdent hotkeys for PSReadline

Here's a couple of key handlers to add indenting and outdenting with Alt [ and ] for PSReadLine.

@Jaykul
Jaykul / Annoying Policies.md
Last active March 27, 2022 00:51
Administrators can remove policies that break their computers

Our IT department still deploys GPO's that, frankly, break things for me. As a developer, I have full admin rights on my laptop. If you have admin rights, you can (temporarily) remove GPO policies.

For example:

  • The Microsoft\FVE policy was breaking Docker: docker/for-win#1297
  • The Microsoft\Edge and Google\Chrome policies force startup options that slow me down

As long as those breaking policies can be removed in the registry, I just fix it by running something like this every time I reboot my computer. I actually stuck this in my profile.ps1, so to make it work I use Start-Process pwsh -Verb RunAs to run it in an elevated session (because I only rarely run PowerShell elevated, but I always start Windows Terminal with a PowerShell session on login).