Skip to content

Instantly share code, notes, and snippets.

@sean-m
sean-m / BeepOnChange.ps1
Created May 8, 2023 15:08
Take pipeline input and beep when the input changes. It will optionally stop when the change occurs.
function BeepOnChange {
<#
.Synopsis
Take pipeline input and beep when the input changes.
It will optionally stop when the change occurs.
.PARAMETER Skip
Number of lines to not consider for comparison. If you're piping
a long running command like ping /t, you want it to skip the
header of the command. Combine ping with the StopOnChange switch
for it to alert when a network connection comes back up and ping
@sean-m
sean-m / StorageConverter.cs
Last active June 5, 2023 21:01
Just sick of looking up this size conversion math. This should be in the BCL.
/*
Usage:
var five = 5000.FromMB();
Console.WriteLine($"{five.ToKB()} KB");
Console.WriteLine($"{five.ToMB()} MB");
Console.WriteLine($"{five.ToGB()} GB");
Console.WriteLine($"{five.ToTB()} TB");
Console.WriteLine($"{five.ToPB()} PB");
@sean-m
sean-m / TimeKeeper.ps1
Last active October 16, 2023 21:54
Helper classes for performance instrumentation in PowerShell scripts. This is not the start and end of performance analysis of scripts, I'm not even saying it's good, it is as consistent and low overhead as I have the energy to make it right now and has helped troubleshoot a real problem at work.
#!/usr/bin/pwsh
#Requires -Version 5
#region TimeKeeper
################################################################################
## TimeKeeper ##
################################################################################
class TimeEvent {
<#
@sean-m
sean-m / CsvFileReader.cs
Last active September 26, 2024 00:03
Helpers for optimistically loading strongly typed data in C# programs. Useful for bulk loading data for unit tests or dev environments. UglyDbInit will use the CsvFileReader to initlaize an entity framework database context.
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic.FileIO;
namespace SMM {
public class CsvFileReader {
string file_path;
private CsvFileReader(string FilePath) {
@sean-m
sean-m / OrderGroupAtFirstOccurance.cs
Last active October 18, 2023 16:59
Extension method that will yield properties from a specified group of properties if any of them are encountered in the first collection. It's using the group metaphore because I use it for grouping properties together by name in a UI. Yes there's perf issues as things get big, it's your foot.
/// <summary>
/// Let's say you have a collection of things with an indeterminate ordering but when you
/// enumerate the things, you'd like a subset to be grouped together. That's what this
/// can do but there's no guarantee elements in the group parameter exist in the primaryCollection.
/// That can be seen as a bug or a feature, it's up to you.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="primaryCollection"></param>
/// <param name="group"></param>
/// <returns></returns>
<?xml version="1.0" encoding="utf-16"?>
<StorableColorTheme xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Keys>
<string>ErrorForegroundColor</string>
<string>ErrorBackgroundColor</string>
<string>WarningForegroundColor</string>
<string>WarningBackgroundColor</string>
<string>VerboseForegroundColor</string>
<string>VerboseBackgroundColor</string>
<string>DebugForegroundColor</string>
@sean-m
sean-m / ToKvText.cs
Last active November 2, 2023 22:45
Takes an object and does a naive pass at turing it into a string that would print well in a console.
public string ToKvText(object entry) {
if (entry == null) return string.Empty;
StringBuilder sb = new StringBuilder();
var properties = entry.GetType().GetProperties();
var longestPropertyLength = properties.Select(x => x.Name.Length).Max();
int indentation = longestPropertyLength + 3;
foreach (var p in properties) {
## Takes and ACL and SID, returns an ACL with the correct entry for read-only permissions added.
function Add-ReadAce {
[OutputType([System.Security.AccessControl.FileSystemSecurity])]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$false)]
[System.Security.Principal.IdentityReference]$SID,
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[System.Security.AccessControl.FileSystemSecurity]$ACL
)
@sean-m
sean-m / srtshift.ps1
Created December 31, 2024 01:55
Apply a timeshift to an srt file and write to stdout.
#!/usr/bin/env pwsh
param($srtFile, $shiftMiliseconds=0)
if (-not $srtFile) {
throw "Must set srtFile path."
return
}
$srtLines = Get-Content $srtFile