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 / Grouping.psm1
Created April 14, 2023 22:18
Expand and Group objects when they have (or could be deduped with) a single array property
filter Expand-Property {
<#
.SYNOPSIS
Expands an array property, creating a duplicate object for each value
.EXAMPLE
[PSCustomObject]@{ Name = "A"; Value = @(1,2,3) } | Expand-Property Value
Name Value
---- -----
A 1
@Jaykul
Jaykul / Invoke-WithEncoding.ps1
Last active February 22, 2023 07:37
Work around binaries that don't respect the console code page
class EncodingAttribute : System.Management.Automation.ArgumentTransformationAttribute {
[object] Transform([System.Management.Automation.EngineIntrinsics]$engineIntrinsics, [object]$inputData) {
return [System.Text.Encoding]::GetEncoding("$inputData")
}
}
function Invoke-WithEncoding {
<#
.SYNOPSIS
Temporarily switch output encoding
@Jaykul
Jaykul / WslInstall.psm1
Last active October 3, 2023 21:16
Install WSL2 distros non-interactively
function Add-WslUser {
<#
.SYNOPSIS
Adds a user to a WSL distro
#>
[CmdletBinding()]
param(
# The distro to add the user to
[Parameter(Mandatory)]
$Distribution,
# Disable shell integration when the language mode is restricted
# Prevent installing more than once per session
if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage" -or (Test-Path variable:global:__VSCodeOriginalPrompt)) {
return;
}
$Global:__VSCodeOriginalPrompt = $function:Prompt
function global:prompt {
$ExitCode = $? ? 0 : ($LASTEXITCODE -ne 0) ? $LASTEXITCODE : 1
@(
@Jaykul
Jaykul / AudioPlaybackEngine.cs
Last active October 8, 2022 05:41 — forked from markheath/AudioPlaybackEngine.cs
FireAndForget NAudio Sample
using System;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
namespace FireAndForgetAudioSample
{
class AudioPlaybackEngine : IDisposable
{
private readonly IWavePlayer outputDevice;
private readonly MixingSampleProvider mixer;
class PSEquality : System.Collections.Generic.EqualityComparer[PSObject] {
[scriptblock]$Comparer = { $_ }
[bool] Equals([PSObject]$first, [PSObject]$other) {
return ($first | ForEach-Object $this.Comparer) -eq ($other | ForEach-Object $this.Comparer)
}
[int] GetHashCode([PSObject]$module) {
return $module.GetHashCode()
}
@Jaykul
Jaykul / Lessons Learned.md
Last active July 13, 2022 06:17
Trying to find out what's the fastest way to count characters ...

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
@Jaykul
Jaykul / Show-Date.ps1
Created July 9, 2022 02:59
Show-Date
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(
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 June 25, 2022 22:48
An Invoke Wrapper For Labs
ComputerName Email UserName Password
NotOne user@gmail.com bob s3cr3t
NotTwo user2@gmail.com sue m04rs3cr3t