Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
ninmonkey / Saving-Excel-Workbook-As-Base64-Round-Trip.pq
Last active April 25, 2025 17:14
Saving-Excel-Workbook-As-Base64-Round-Trip.pq
let
Path_Xlsx = "c:\data\workbook.xlsx",
Summary = [
// [1] using "enter-data" saves a table as json like this
b64_str = "i45WSkosUoqNBQA=",
bytes = Binary.FromText( b64_str, BinaryEncoding.Base64),
bytes_decompressed = Binary.Decompress( bytes, Compression.Deflate),
final_json_str = Text.FromBinary( bytes_decompressed, TextEncoding.Utf8 ),
// [2] converting some binary file to base64
@ninmonkey
ninmonkey / PwshClass▸Using▸Text.Json.ps1
Last active April 20, 2025 21:06
AutoJson.Basic-Text.Json.md
#Requires -Version 7
using namespace System.Collections.Generic
using namespace System.Text
using namespace System.Text.Json
using namespace System.Text.Json.Serialization
using namespace System.Linq
$assembly = Add-type -AssemblyName System.Text.Json -PassThru -ea 'stop'
<#
@StartAutomating
StartAutomating / ThereIsNoRouteTable.ps1
Created March 22, 2025 16:13
Gist a web server without a route table
<#
.SYNOPSIS
A pure PowerShell web server
.DESCRIPTION
A pure PowerShell web server proof of concept.
This creates a simple web server that routes commands directly by their local path
That is `/hello` would run the function `/hello` if it exists.
@ninmonkey
ninmonkey / Dynamic Winget Arguments.pwsh.ps1
Last active January 11, 2025 22:44
Dynamic Winget Arguments using Powershell Pwsh
function InvokeWingetInstall {
<#
.synopsis
example of using native commands with dynamic conditions.
.example
# example commands: Use -TestOnly and -Verbose to see cli arguments, without invoking it.
InvokeWingetInstall -Verbose -Package 'Microsoft.PowerBI' -TestOnly -Silent
VERBOSE: install --id Microsoft.PowerBI /silent
@StartAutomating
StartAutomating / EventBasedServer.ps1
Last active April 9, 2025 22:42
Gist a small event-based HTTP server in PowerShell
$JobName = "http://localhost:$(Get-Random -Min 4200 -Max 42000)/"
$httpListener = [Net.HttpListener]::new()
$httpListener.Prefixes.Add($JobName)
$httpListener.Start()
Start-ThreadJob -ScriptBlock {
param($MainRunspace, $httpListener, $SourceIdentifier = 'http')
while ($httpListener.IsListening) {
$contextAsync = $httpListener.GetContextAsync()
while (-not ($contextAsync.IsCompleted -or $contextAsync.IsFaulted -or $contextAsync.IsCanceled)) {}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Management.Automation;
using Microsoft.PowerShell.Commands;
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract class CmdletWithPathBase : PSCmdlet
@ninmonkey
ninmonkey / Json With Nan Comparison.md
Last active December 6, 2024 16:11
ConvertTo-Json using Nan and Infinity Comparison

About

Compare if NaN fails a round trip in Powershell or when using System.Text.Json

Ran using: pwsh.exe 7.4. context

Tests

@Jaykul
Jaykul / WrapString.ps1
Created October 5, 2024 17:35
String Wrapping. Again.
# Borrowed this one from https://github.com/chalk/ansi-regex
$script:AnsiPattern = "[\u001b\u009b][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d\/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?(?:\u001b\u005c|\u0007))|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))"
$script:AnsiRegex = [Regex]::new($AnsiPattern, "Compiled");
function MeasureString {
[CmdletBinding()]
param(
[string]$InputObject
)
$AnsiRegex.Replace($InputObject, '').Length
@Jaykul
Jaykul / gcixel.ps1
Created September 1, 2024 04:55
ImageMagick and Sixels -- this is not fast, but it works even across ssh
<#
.SYNOPSIS
gcixels is like gci, but with sixels...
.DESCRIPTION
Shows thumbnails of images with titles, directly in terminal.
However, it's done with ImageMagick montage, so it's awfully slow.
Just sharing it for your inspiration.
.NOTES
Requires ImageMagick and a Sixel terminal (like Windows Terminal 1.22+ or WezTerm)
#>
#Requires -PSEdition Desktop
[CmdletBinding()]
param (
[ValidateSet('AllUsers', 'CurrentUser')]
[string]
$Scope = 'CurrentUser'
)
$ErrorActionPreference = 'Stop'