Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
ninmonkey / Find Archived CDC Files without Javascript.ps1
Created February 1, 2025 17:50
Find archived urls. This method requires no javascript/session/headless browser, etc. 2025/01
using namespace System.Collections.Generic
<#
Using: Pwsh 7
Related:
For parsing HTML with CSS Selector queries, see this template: <https://gist.github.com/ninmonkey/17f6a1f5b28249b6cb2ba8bc746ae4fb>
#>
function FindArchivedUrl {
<#
@ninmonkey
ninmonkey / Hashtable Collisions Differ When Using Literals.md
Last active January 28, 2025 16:26
Colliding Keys Test - Literals don't error and don't lose values

About

I'm experimenting with collisions for the Env var RFC: PowerShell/PowerShell-RFC#384

Cases

Case: fails as expected

$h1 = @{ "path"      = 'foo\bar' }
$h2 = @{ "path`u{0}" = '' }
@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
@ninmonkey
ninmonkey / Compare Autoloading Commands.ps1
Last active January 4, 2025 21:48
Test autoloading pwsh modules when using Get-Command and Get-Module
# I'm explicitly unloading the module for each test
Remove-Module ImportExcel -ea 'ignore'
(Get-Module ImportExcel).IterCommands.count
# out: 0
Remove-Module ImportExcel -ea 'ignore'
(Get-Module ImportExcel).count
# out: 0
Remove-Module ImportExcel -ea 'ignore'
@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

@ninmonkey
ninmonkey / DAX using fancy CSS Variables instead of strings.md
Last active January 8, 2025 19:01
Appending strings in DAX templates does not scale well, for the human

SVG's support CSS variables.

CSS variables

Instead of hard coding values in the whole file:

<path fill="#123456" .... >

You can declare a variable named --color-main and resuse it.

@ninmonkey
ninmonkey / Compare Conversions using numeric literals and operators.ps1
Last active November 2, 2024 21:16
Test which conversions occur in pwsh numeric literals
@'
There's a bunch of literals added in 6+
7.1 added
> using a type suffix on a hex literal now returns a signed value of that type
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_numeric_literals?view=powershell-7.5
'@
100 - 93.79 | ty 'i int, i d'
@ninmonkey
ninmonkey / Text to bytes to and HexString, round trip.ps1
Last active November 2, 2024 20:11
Nonsense example. Just showing a round trip.
$originalMessage = "hi 🐒 world"
$enc = [Text.Encoding]::GetEncoding('utf-8')
$bytes = $enc.GetBytes( $originalMessage)
$hexStr = [Convert]::ToHexString( $bytes )
$asBytes = [Convert]::FromHexString( $hexStr )
$roundTrip = $enc.GetString( $AsBytes )
[pscustomobject]@{
@ninmonkey
ninmonkey / Base Version pwsh7 does not break.ps1
Last active October 24, 2024 21:46
WinPS: Calls Add-Content with multiple retries. Before failing
# This is a ps5 + ps7 script
# ps5 writes errors when PBI refreshes, ps7 either never does, or its much, much more rare
$dest = 'C:\Users\cppmo_000\AppData\Local\Temp\cults.csv'
$iter = 0
while($true) {
$iter++
$Last = [datetime]::Now
$content = w32tm /stripchart /computer:time.google.com /samples:1 /dataonly
Add-Content -Path $dest -Value (
@ninmonkey
ninmonkey / Pwsh Line Continuations are Permissive.md
Last active October 17, 2024 17:40
Pwsh Line Continuations are Permissive

Sometimes line continuations are quite permissive

Starting code

[datetime]::ParseExact( 'septiembre', 'MMMM', (get-culture 'es') ).ToString('MMMM', (get-culture 'es'))

[a]