Skip to content

Instantly share code, notes, and snippets.

View gregmac's full-sized avatar

Greg MacLellan gregmac

View GitHub Profile
private static string[] ByteUnits = new string[] { "B", "KB", "MB", "GB", "TB" };
/// <inheritdoc cref="BytesToString(decimal, byte)"/>
public static string BytesToString(this int? value, byte decimals = 2) => BytesToString((decimal)value, decimals);
/// <summary>
/// Display a byte value with units, rounded up to nearest possible size unit.
/// </summary>
/// <param name="value">Number of bytes</param>
@gregmac
gregmac / timestamp-log
Created October 23, 2020 06:35
timestamp-log bash script
#!/bin/bash
# simple script to take stdin, timestamp it, and optionally append to a log file
# usage: command | timestamp-log /path/to/file.log
if [[ ! -z "$1" ]]; then
echo "Logging to $1"
exec > >(tee -ia $1)
exec 2> >(tee -ia $1)
fi
@gregmac
gregmac / OhMyPosh SetupGuide.md
Last active April 28, 2023 15:35
Oh-my-posh Config
  • Originally adapteed from Scott Hanselman's excellent guide.
  • Updated in 2021-08 for oh-my-posh v3.
  • Uses a theme based on Paradox theme
    • Short folder names, special icons for Registry, Home, etc
    • Different prompt icon when running as admin
    • Shows last exit code
    • Timing info for long-running commands
    • Git status

@gregmac
gregmac / ProcessInfo.ps1
Created December 12, 2019 23:12
Get CPU, Memory and Command Line using PowerShell
$sampleTime = 2; # timespan to use for CPU%
$cmds = @{}; Get-CimInstance Win32_Process | %{ $cmds[$_.ProcessId.ToString()] = $_.CommandLine; };
$cpu1 = @{}; Get-Process | %{ $cpu1[$_.Id.ToString()] = $_.CPU }
start-sleep -seconds $sampleTime
Get-Process | select `
Id, `
Name, `
@{Name="CPU";Expression={ (($_.CPU-$cpu1[$_.Id.ToString()]) / $sampleTime / [System.Environment]::ProcessorCount / 100) }}, `
WorkingSet64, `
@{Name="CommandLine";Expression={$cmds[$_.Id.ToString()]}} `
@gregmac
gregmac / bootstrap-cfn-init.ps1
Created November 21, 2019 19:17
Bootstrap AWS CloudFormation::Init
# Windows Powershell script to bootstrap AWS CloudFormation:Init
# This can be used from a userdata script (in the <powershell> section)
# * Sets up cfn-hup to automatically run when changes are made to an assoicated LaunchConfiguration
# * Does initial invocation of cfn-init
$instanceId = (Invoke-WebRequest http://169.254.169.254/latest/meta-data/instance-id).Content
$region = (Invoke-WebRequest http://169.254.169.254/latest/meta-data/placement/availability-zone).Content -replace ".$"
$tags = (Get-EC2Instance -InstanceId $instanceId -Region $region).RunningInstance.Tags
$autoscalingGroupName = ($tags | Where-Object {$_.Key -eq "aws:autoscaling:groupName"}).Value
$stackId = ($tags | Where-Object {$_.Key -eq "aws:cloudformation:stack-id"}).Value
$launchConfigurationName = (Get-ASAutoScalingGroup -AutoScalingGroupName $autoscalingGroupName -Region $region).LaunchConfigurationName
@gregmac
gregmac / Ping2.ps1
Created November 18, 2019 21:07
Powershell Ping2
# better long-running ping command (timestamped output, but no stats)
function Ping2
{
param(
[Parameter(Mandatory=$true,Position=0)][string]$Target,
[int]$Timeout = 1000,
[int]$Interval = 1000,
@gregmac
gregmac / EnableNLog.cs
Last active October 17, 2019 15:07
NLog logging configuration for LINQPad
{
NLog.LogManager.Configuration = new NLog.Config.LoggingConfiguration();
NLog.LogManager.Configuration.AddTarget("console", new NLog.Targets.ConsoleTarget());
NLog.LogManager.Configuration.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, "console");
NLog.LogManager.ReconfigExistingLoggers();
}
@gregmac
gregmac / output.md
Last active October 2, 2019 17:08
.NET FIPS test

FIPS testing

FIPS mode on

SHA256

SHA256.Create()

9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08

@gregmac
gregmac / $PROFILE\Microsoft.PowerShell_profile.ps1
Last active December 12, 2019 02:06
PowerShell Set-WindowTitle profile config
# Import posh-git (if installed via scoop)
$poshGitModule = "$HOME\scoop\apps\posh-git\current\posh-git.psd1";
if (Test-Path $poshGitModule) { Import-Module $poshGitModule }
# msbuild convenience alias
Set-Alias MSBuild 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe'
# bash-style completion
Set-PSReadlineKeyHandler -Key Tab -Function Complete
#Set-PSReadlineOption -ShowToolTips
{
"homepage": "https://github.com/RolandPheasant/TailBlazer",
"version": "0.9.0.536",
"license": "GPL-3.0-only",
"url": "https://github.com/RolandPheasant/TailBlazer/releases/download/0.9.0.536/Tailblazer_v0.9.0.536.zip",
"hash": "53341485375b0a9ebd4085dbafa71c68af9a2044ff1bb65f3300be4b43f6c11c",
"bin": "TailBlazer.exe",
"shortcuts": [
[
"TailBlazer.exe",