Skip to content

Instantly share code, notes, and snippets.

Avatar
😀
Learning

Joel Bennett Jaykul

😀
Learning
View GitHub Profile
@Jaykul
Jaykul / PSReadlineHotkeys.ps1
Last active Jul 2, 2021
Indent and Outdent hotkeys for PSReadline
View PSReadlineHotkeys.ps1
# Hack in a [ReadLine] accelerator
$xlr8r = [psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")
if ($xlr8r::AddReplace) {
$xlr8r::AddReplace("ReadLine", [Microsoft.PowerShell.PSConsoleReadLine])
} else {
$null = $xlr8r::Remove("ReadLine")
$xlr8r::Add("ReadLine", [Microsoft.PowerShell.PSConsoleReadLine])
}
# Now some key handlers
@Jaykul
Jaykul / A retro look.md
Last active Apr 20, 2021
Retro Terminal
View A retro look.md

I borrowed crt.hlsl from Hammster although I've tweaked it a little.

You need a terminal profile with something like this in it:

{
    // This is the shader and a background with the monitor in it, because that's easy
    "experimental.pixelShaderPath": "C:\\Users\\Jaykul\\crt.hlsl",
    "backgroundImage": "C:\\Users\\Jaykul\\crt.png",
    "backgroundImageAlignment": "center",
@Jaykul
Jaykul / About Non-Mandatory Dynamic Parameters.md
Last active Mar 2, 2021
Sometimes you need that parameter to be mandatory, but the rest of the time, you use an environment variable.
View About Non-Mandatory Dynamic Parameters.md

When you're writing scripts that are primarily meant for use in continuous integration or deployment environments, a lot of your inputs (maybe all of your inputs) are going to come from environment variables that are always set when you're running the scripts in production (but which rarely exist on your development box). There are a lot of different ways of handling this, but the one I find most elegant is to use PowerShell's dynamic parameter block.

Dynamic parameters in PowerShell have a couple of downsides (which, now that I think of it, we should really see about fixing, since PowerShell is open source).

  • The first is that they are not easily discoverable. Even when you're in the conditions where the dynamic parameters will be emitted, they do not show up in either Get-Help or Get-Command (nor even in Get-Parameter, which does fetch dynamic parameters added by providers, but doesn't find the ones added by the command itself).
  • The second is that they don't set variables in the function
@Jaykul
Jaykul / Invoke-WithRetry.ps1
Created Jan 20, 2021
There are so many ways to write this...
View Invoke-WithRetry.ps1
function Invoke-WithRetry {
<#
.SYNOPSIS
Invoke a ScriptBlock until it returns a value (or until a certain ammount of time elapses)
.DESCRIPTION
Invokes the given ScriptBlock on $RetryEvery interval until it returns a value.
If the $Limit time passes without a value, returns nothing.
.EXAMPLE
$Time = (Get-Date).AddMinutes(0.5)
Invoke-WithRetry { if($Time -lt (Get-Date)) { return $Time } }
@Jaykul
Jaykul / ProgrammerNotebooks.ipynb
Last active Jan 14, 2021
Interactive Coding Notebooks: The Swiss Army Knife of data programming
View ProgrammerNotebooks.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Jaykul
Jaykul / About the better CredentialAttribute.md
Last active Dec 18, 2020
You really should be able to control the prompt ...
View About the better CredentialAttribute.md

Here's something I was inspired to write by a thread on twitter this morning: a better [Credential()] attribute. It allows you to configure the prompt that is presented to the user if they only provide a name -- and by specifying a default value (even an empty string), to make the credential mandatory while still controlling the prompt:

function Test-Credential {
[CmdletBinding()]
param(
    [Parameter(Mandatory)]
    [BetterCredential(Title="Mandatory Credential", Prompt="Please provide the password for the TeamLab", MandatoryPassword)]
    [PSCredential]$Credential = "TeamAdmin"
)
@Jaykul
Jaykul / Get-HelpInfo.ps1
Created Dec 12, 2020
Some simple analysis of updateable help
View Get-HelpInfo.ps1
filter Get-HelpInfo {
<#
.SYNOPSIS
Get a ((list of) en-US culture-specific?) HelpInfo details (the URL and version number) for modules
.EXAMPLE
Get-Module Microsoft.* -ListAvailable | Get-HelpInfo
#>
param(
[Parameter(ValueFromPipeline)]
[System.Management.Automation.PSModuleInfo]$Module = $((Get-Module WindowsUpdateProvider -List)[0])
View Expand-StringVariables
function Expand-AstVariables {
param (
[Parameter(Mandatory, ValueFromPipeline)]
[System.Management.Automation.Language.Ast] $Statement,
[Parameter(Mandatory)]
[ref] $result,
[Parameter(Mandatory)]
[PSObject] $Properties
)
@Jaykul
Jaykul / Packer.ps1
Last active Jun 14, 2021
There are so many ways to embed code (dlls OR script)
View Packer.ps1
using namespace System.IO
using namespace System.IO.Compression
using namespace System.Text
[CmdletBinding()]
param(
[string]$Path = $PSCommandPath
)
function CompressToString {
<#
@Jaykul
Jaykul / Measure-Script.ps1
Created Oct 22, 2020
We need a better Measure-Command which can show averages
View Measure-Script.ps1
filter Measure-Script {
<#
.SYNOPSIS
A wrapper for Measure-Command to run commands multiple times and report averages (avg, min, max)
#>
param(
# The script to measure
[Parameter(ValueFromPipeline)]
[ScriptBlock]$Expression,