Skip to content

Instantly share code, notes, and snippets.

@sean-m
sean-m / TimeEstimator.ps1
Last active December 4, 2023 22:15
A time estimator for use with PowerShell's Write-Progress. Provided a total number of cycles, it tracks how long the last 100 or less itterations of your main loop took and calculates the remaining time based on velocity. Note: when ticks exceed total, it returns a seconds remaining of 0, but continues to track the rate that work is getting done.
<#
A time estimator for use with PowerShell's Write-Progress. Provided a total
number of cycles, it tracks how long the last 100 or less itterations of your
main loop took and calculates the remaining time based on velocity. Note: when
ticks exceed total, it returns a seconds remaining of 0, but continues to track
the rate that work is getting done.
The intended use case is with Write-Progress, calls to that cmdlet are really
slow on PowerShell 2-5, efforts have been made to maintain low overhead. If
you're after performance this is still useful, just log the progress yourself.
For instance, using [System.Diagnostics.Trace]::Write() and watching with
@sean-m
sean-m / LoadUsersIntoLdap.ps1
Created August 5, 2022 18:05
For loading users into an ldap directory from a CSV file. Will create new users and update attributes as needed on existing users.
#Requires -Module ActiveDirectory
[CmdletBinding()]
param ($users_file, [switch]$WhatIf)
if (-not $users_file) {
Write-Warning "Must pass file name as `$users_file"
return 1
}
Write-Host -ForegroundColor Cyan $users_file
@sean-m
sean-m / CsvFileReader.cs
Last active July 25, 2023 19:13
Simple csv file reader. Does not handle malformed files where some rows have an incomplete numbert of elements. It's meant to only read in test data in debug mode. Dumb to include a package to do this when it shouldn't be included in the release anyhow.
using System.Reflection;
using System.Text.RegularExpressions;
using McAttributes.Data;
using Microsoft.VisualBasic.FileIO;
namespace SMM
{
public class CsvFileReader
{
static string csv_delimiter_pattern = @"(?:^|,)(?=[^""]|("")?)""?((?(1)(?:[^""]|"""")*|[^,""]*))""?(?=,|$)";
@sean-m
sean-m / Test-SemanticEquality.ps1
Last active May 2, 2022 19:04
Semantic comparison of deserialized JSON in PowerShell. Purposefully ignores array element ordering. I think this works. Needs testing on a larger corpus.
#Requires -Modules Pester
function Test-SemanticEquality {
<#
.Synopsis
When comparing two JSON records, comparing the string representation of one
to the other can fail for any nubmer of reasons. This function takes parsed
JSON records and compares their structure/values to tell if the sources
are semantically equivalent.
.DESCRIPTION
@sean-m
sean-m / db.sh
Created April 14, 2022 00:59
Script to spin up a postgresql db for local development using podman.
#!/usr/bin/env bash
set -e
here=$(dirname "$0")
pushd "$here" > /dev/null
prep ()
{
if [ ! -d data ]; then
@sean-m
sean-m / ADTS_AC_Guids.ps1
Last active July 29, 2021 22:59
To ease in creating odd ACL entries in AD.
$ADTS_AC_Guids = @{
'Abandon-Replication'=[Guid]"ee914b82-0a98-11d1-adbb-00c04fd8d5cd"
'Add-GUID'=[Guid]"440820ad-65b4-11d1-a3da-0000f875ae0d"
'Allocate-Rids'=[Guid]"1abd7cf8-0a99-11d1-adbb-00c04fd8d5cd"
'Allowed-To-Authenticate'=[Guid]"68b1d179-0d15-4d4f-ab71-46152e79a7bc"
'Apply-Group-Policy'=[Guid]"edacfd8f-ffb3-11d1-b41d-00a0c968f939"
'Certificate-Enrollment'=[Guid]"0e10c968-78fb-11d2-90d4-00c04f79dc55"
'Certificate-AutoEnrollment'=[Guid]"a05b8cc2-17bc-4802-a710-e7c15ab866a2"
'Change-Domain-Master'=[Guid]"014bf69c-7b3b-11d1-85f6-08002be74fab"
'Change-Infrastructure-Master'=[Guid]"cc17b1fb-33d9-11d2-97d4-00c04fd8d5cd"
@sean-m
sean-m / IRule.ps1
Last active July 20, 2021 00:01
Simple rule matcher for PowerShell. Allows writing logic as a class that will match any or all of the predicates and optionally return a value on match.
class IRule {
[System.Collections.Generic.IEnumerable[IRule]]$Rules
# Or, Any : bail on first match
# And, All : evaluate all rules, only return a result if they all succeed
[ValidateSet("Or","Any", "And", "All")]
$MatchType
# Called on matching rules, overload in an inherited class to return
# values relevant to that rule.
@sean-m
sean-m / HotCache.ps1
Created March 23, 2021 23:39
Simple static cache for PowerShell. Intend to add an object limit and generational eviction mechanism. For right now it's basically a cleaner way to use a hashtable for caching inside a script.
class CacheResolverException : Exception {
CacheResolverException ([string]$Message, [Exception]$InnerException) : base ($Message, $InnerException) { }
}
class HotCache {
hidden static [Hashtable]$Records = @{}
hidden [UInt64] $Hit = 0
hidden [UInt64] $Miss = 0
hidden [int] $Limit = 0
@sean-m
sean-m / invoke_sql.ps1
Last active December 15, 2020 18:12
Modified version of a function posted to Technet, this one attempts to handle GO statements so can take auto generated SQL from SSMS.
function Invoke-SQL {
param(
[string] $connectionString = ".\SQLEXPRESS",
[string] $sqlCommand = $(throw "Please specify a query."),
[hashtable] $parameters = @{},
[switch] $splitOnGo
)
$queries = @()
## Split sqlCommand on GO keyword.
@sean-m
sean-m / sweep.py
Last active September 23, 2020 00:05
sloppy minesweeper in with python3 tkinter
#! /usr/bin/env python3
import random as rand
import tkinter as tk
from tkinter import ttk
colors = [ "white", "blue", "green", "red", "purple", "brown", "grey", "black" ]
class Tile:
def __init__(self, bomb=False):