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 / Agent Passthru.md
Last active April 18, 2024 11:18
SSH Agent passthru to WSL 2 (working, in Windows 11, in May 2023)

For awhile now, each time I got a new Windows laptop I would dig up strasis gist on how to set up agent forwarding for SSH in WSL2 -- but recently I tried to point someone else at it and they were very confused by it, so this is my attempt at simpler instructions.

Installation

With Chocolatey, you must use an elevated PowerShell session. If there's no choco command found, it will fall back to winget for the npiperelay install. To force using Winget even if you have choco installed, you need to download it, so you can pass parameters to it.

Easy mode: just run this in PowerShell:

@Jaykul
Jaykul / EngineEvent.ps1
Last active April 5, 2024 16:45
How to inject text into the NEXT command prompt
$global:Suggestion = Get-Completion "This is my question"
$global:InjectCommand = Register-EngineEvent -SourceIdentifier PowerShell.OnIdle {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($global:Suggestion)
Stop-Job $global:InjectCommand # This removes the event
}
@Jaykul
Jaykul / Blog: Installing Docker.md
Last active March 7, 2024 09:56
Docker in PowerShell on Windows 10

Using Docker on Windows 10 (Updated at the end of 2019)

Since I wrote this originally, Docker on Windows has become a first-class supported tool, with a Windows Installer and well-documented installation processes from docker and from Microsoft.

Today, I actually install docker using boxstarter scripts where I can Enable-WindowsOptionalFeature -Online -FeatureName containers -All and then choco upgrade -y docker-desktop as well as installing tooling for VS Code code --install-extension "ms-azuretools.vscode-docker".

I've left the rest of these notes here as a historical record, more than anything else. You should not expect the script below to work, but you certainly don

@Jaykul
Jaykul / New-Hyperlink.ps1
Last active February 16, 2024 19:00 — forked from JustinGrote/Write-ANSIHyperlink.ps1
Powershell Function to create Terminal HyperLinks using ANSI Escape Sequences (Supported in Windows Terminal PREVIEW)

This is barely worth sharing, but you can, for instance, get everything pinned to the taskbar, and unpin it from the start menu

Get-Application -PinnedToTaskbar | UnpinStart

Huge Caveat: I haven't figured this all out.

  1. I can't figure out a way to get the order of the taskbar items...
  2. Many items don't have the "pin to taskbar" verb when called this way (I don't know why) and you have to do something ridiculous like making a shortcut to them first (thus, the task bar folder) and then pin that.
@Jaykul
Jaykul / You Need To Implement Non-Generic.md
Created April 27, 2016 01:48
Implementing IEnumerator<T> in PowerShell

In order to implement IEnumerator<T> you have to explicitly implement the Current member for IEnumerator<T> and IEnumerator ... but PowerShell won't let you have two different implementations of the same property, nor will it let you explicitly implement an interface member. So we do one at a time, like this:

First, make a non-generic IEnumerator, but implemented with the type you want in the end:

    class __Generator : System.Collections.IEnumerator {
        [int]$Actual = 0

        [object]get_Current() {
            return $this.Actual
@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 / WhoMovedMyCheese.ps1
Created November 13, 2015 20:58
Exceptions In PowerShell
#4 I call this one: where the bleep did they put the cheese?
function Get-FileException {
param($Path)
# Assume I'm doing something with files and it throws this exception:
$fnf = [System.IO.FileNotFoundException]::new("File Not Found", $Path)
$inf = [System.Management.Automation.ItemNotFoundException]::new( "Item Not Found", $fnf)
throw $inf
}
@Jaykul
Jaykul / About Batch and PowerShell.md
Last active January 24, 2024 05:27
Executable PowerShell (wrap a ps1 and name it cmd)

This is always my answer to all those "compile your .ps1" solutions that are floating around. Why would you wrap your PowerShell in an exe and some custom host?

Just paste this header on the top of your PowerShell script, and then before you share it, rename it with the .cmd extension. While you're writing, it's a nice syntax-highlighted PowerShell script, and when you rename it, *poof* it's double-clickable. Drop it in the PATH and you can execute it from the Run dialog or from PowerShell, batch, whatever.

Because there are still people around who actually use CMD, I've updated it to show how to handle passing parameters.

A couple of notes:

  1. It runs with ExecutionPolicy unrestricted because if you're still using CMD you probably haven't configured your ExecutionPolicy
  2. It uses -NoProfile to make sure the environment is the same on everyone's PC...
  3. It only creates function :: {} because that allows us to ignore the :: on the first line
@Jaykul
Jaykul / Invoke-Native.psm1
Last active January 9, 2024 13:40
Calling native executables that write non-error to stderr (with prefixes like "Warning:" and "Error:") ...
class EncodingAttribute : System.Management.Automation.ArgumentTransformationAttribute {
[object] Transform([System.Management.Automation.EngineIntrinsics]$engineIntrinsics, [object] $inputData) {
if ( $inputData -is [string] ) {
return [Text.Encoding]::GetEncoding($inputData)
} else {
return $inputData
}
}
}