Skip to content

Instantly share code, notes, and snippets.

View StartAutomating's full-sized avatar

James Brundage StartAutomating

View GitHub Profile
@StartAutomating
StartAutomating / MovingAverage.ps1
Created March 30, 2025 23:28
Gist using the object pipeline to calculate a moving average
function Get-MovingAverage {
<#
.SYNOPSIS
Gets a moving average.
.DESCRIPTION
Gets a moving average of numbers passed to it.
#>
[Alias('MovingAverage')]
param(
# The number we're averaging.
@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.
@StartAutomating
StartAutomating / TextToSpeech.ps1
Created February 25, 2025 00:36
Gist a bit of Text to Speech in PowerShell
param(
[string]
$Message = "Coming at you live from the uncanny valley"
)
if ($IsUnix -or $IsMacOS) {
throw "SAPI not available on Unix/MacOS"
}
Start-ThreadJob -ScriptBlock {
$sapi = New-Object -ComObject Sapi.SpVoice
@StartAutomating
StartAutomating / FunctionsInABackgroundJob
Created January 26, 2025 21:22
Gist showing how to declare functions in a background job
Start-ThreadJob {param($V)
foreach ($kv in $v.GetEnumerator()) {
$ExecutionContext.SessionState.PSVariable.Set($kv.Key, $kv.Value)
}
testing
} -Arg @{'function:testing' = {1..3}} | Wait-Job | Receive-Job
@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)) {}
@StartAutomating
StartAutomating / GetRandomColor.ps1
Created December 27, 2024 00:33
Gist a little random color generator in PowerShell
"#$('{0:x6}' -f (Get-Random -Maximum 0xFFFFFF))"
@StartAutomating
StartAutomating / FindFriday13th
Last active December 13, 2024 18:25
Gist gotta calculate Friday the 13th
function f13($n=13) {
$day, $f13 = (Get-Date).Date, @()
do {
if ($day.DayOfWeek * $day.Day -eq 65) {
$f13 += $day
}
$day = $day.AddDays(1)
} while ($f13.Count -lt $n)
$f13
}
@StartAutomating
StartAutomating / GetLuma.ps1
Created October 18, 2024 05:17
Gist tell me how bright it is
filter GetLuma {
$colorString = $_
# Convert the background color to a uint32
$rgb = ($colorString -replace "#", "0x" -replace ';') -as [UInt32]
# then make it into a percentage red, green, and blue.
$r, $g, $b = ([float][byte](($rgb -band 0xff0000) -shr 16)/255),
([float][byte](($rgb -band 0x00ff00) -shr 8)/255),
([float][byte]($rgb -band 0x0000ff)/255)
# Calculate the luma of the background color
@StartAutomating
StartAutomating / HistoryCompleter.ps1
Created October 17, 2024 05:35
Gist a quick history ArgumentCompleter
# A function without a [CmdletBinding] attribute or [Parameter] attributes is freeform.
# Let's declare a little function that just outputs the name and arguments
function I {
param()
@($MyInvocation.InvocationName) + $args
}
# We can use Register-ArgumentCompleter to register the completer for a named command.
# This simple completer will show any matching history items
Register-ArgumentCompleter -CommandName I -ScriptBlock {
@StartAutomating
StartAutomating / NotOnAFriday.ps1
Created July 27, 2024 00:36
Gist a little friendly advice about Shipping Software
<#
.Synopsis
Not On a Friday.
.Description
Gist a little bit of friendly advice about the wisdom of shipping software on a Friday.
.Notes
Yes, this runs.
#>
[ValidateScript({
if ([DateTime]::Now.DayOfWeek -eq 'Friday') {