Skip to content

Instantly share code, notes, and snippets.

View StartAutomating's full-sized avatar

James Brundage StartAutomating

View GitHub Profile
@StartAutomating
StartAutomating / UsefulAttributes.md
Last active August 7, 2024 01:04
Potentially Useful Attributes in PowerShell

PowerShell, as a language, is very self-discoverable.

When you Get-Command, you get a rich object about a command.

This can have a lot of metadata.

You can and should also use inline help (like this script does).

Sometimes, I want more metadata than the command or parameter can actually provide.

@StartAutomating
StartAutomating / FindReadWriteContainerMounts.ps1
Last active June 7, 2024 17:37
Find Read/Write Mounts in a Container
# When run within a container, this will output the directories that are mounted as read/write.
(Select-String "\S+\s(?<p>\S+).+rw,.+symlinkroot=/mnt/host" "/proc/mounts").Matches.Groups |
Where-Object Name -eq p |
Get-Item -path { $_.Value }
# Hope This Helps! / Have Fun!
@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') {
@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 / 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 / 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 / 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 / EventBasedServer.ps1
Last active June 11, 2025 20:31
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 / 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 / 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