Skip to content

Instantly share code, notes, and snippets.

Avatar
😀
Learning

Joel Bennett Jaykul

😀
Learning
View GitHub Profile
@Jaykul
Jaykul / Lessons Learned.md
Last active Jul 13, 2022
Trying to find out what's the fastest way to count characters ...
View Lessons Learned.md

The main thing I re-learned doing this is: the more you can do in compiled C# code, the faster it will run. Writing a for-loop in C# is absolutely the fastest way to iterate over characters -- even when you have to call it as a static method.

This means that the built-in .Split(<char>) method is the fastest way to count a specific character (not necessarily the most memory efficient).

Here's the results on my local computer:

Average          Command
-------          -------
00:00:00.0000084 &lt;# hardcoded value shows overhead #&gt; 4
View Show-Date.ps1
function Show-Date {
<#
.SYNOPSIS
Get the time span elapsed during the execution of command (by default the previous command)
.DESCRIPTION
Calls Get-History to return a single command and returns the difference between the Start and End execution time
#>
[OutputType([string])]
[CmdletBinding(DefaultParameterSetName = "SimpleFormat")]
param(
View Get-Date.ps1
function Get-Date {
<#
.SYNOPSIS
Get the time span elapsed during the execution of command (by default the previous command)
.DESCRIPTION
Calls Get-History to return a single command and returns the difference between the Start and End execution time
#>
[OutputType([string])]
[CmdletBinding(DefaultParameterSetName = "SimpleFormat")]
param(
@Jaykul
Jaykul / Computers.csv
Last active Jun 25, 2022
An Invoke Wrapper For Labs
View Computers.csv
ComputerName Email UserName Password
NotOne user@gmail.com bob s3cr3t
NotTwo user2@gmail.com sue m04rs3cr3t
@Jaykul
Jaykul / About joining strings.md
Last active Jun 28, 2022
StringBuilder vs += vs -join @()
View About joining strings.md

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
The most items, in the smallest space. And it works with strings.
View About_Format-Wrap.md

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 Apr 1, 2022
Search for repositories and get the latest tags
View Get-AcrTag.ps1
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 Feb 28, 2022
Some functions I wrote to fix WSL problems
View WslHelper.psm1
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 Feb 20, 2022
Collect named capture groups to allow parsing text even with uneven properties
View Select-CapturedString.ps1
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 Mar 27, 2022
Adding Attributes to Get-Member
View Add Attributes On Members.md

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.