Skip to content

Instantly share code, notes, and snippets.

@badmotorfinger
badmotorfinger / psmo.ps1
Last active February 12, 2024 00:18
Generates scripts for most SQL Server database objects using PowerShell (SMO objects)
################################################################################################################################
#
# Script Name : SmoDb
# Version : 1.0
# Author : Vince Panuccio
# Purpose :
# This script generates one SQL script per database object including Stored Procedures,Tables,Views,
# User Defined Functions and User Defined Table Types. Useful for versionining a databsae in a CVS.
#
# Usage :
@badmotorfinger
badmotorfinger / create_directory_hash.ps1
Created April 28, 2023 20:50
Generates a single hash from the contents of a directory
# Define the directory to hash
$dir = "C:\Path\To\Directory"
# Define the hash algorithm to use
$hashAlgorithm = "SHA256"
# Get all files in the directory (including subdirectories)
$files = Get-ChildItem -Path $dir -Recurse | Where-Object {!$_.PSIsContainer}
# Create an empty hash object
# sudo -s
# Then (do not resize the terminal window. Seems to affect the ability to accept the MS EULA):
# curl -Lks https://gist.githubusercontent.com/badmotorfinger/b8f0665125629ed105cb0f3574a20713/raw/eee6723994c3a07d51f13e004a58ebeaaea18b19/fresh-install.sh | /bin/bash
# Prevent Windows and Linux fighting over time. Prevent Linux from changing time on the mobo
timedatectl set-local-rtc 1 --adjust-system-clock
# Proton VPN
@badmotorfinger
badmotorfinger / gist:5575139
Created May 14, 2013 10:55
Formats all SQL scripts in a directory using a free webservice provided by tsqltidy.com and PowerShell.
$uri = 'http://www.tsqltidy.com/SQLTidy.asmx'
$proxy = New-WebServiceProxy -Uri $uri -class FormatSQL -Namespace FormatSQL
#Warning - Will format file contents and overwrite the original file. Use with caution.
gci *.sql | % { $content = $proxy.ParseSQL((get-content $_.FullName)); Set-Content $_.FullName $content; }
@badmotorfinger
badmotorfinger / dupfinder.ps1
Last active July 23, 2022 19:59
Use ReSharper command line tool dupFinder to produce a report of duplicated C# code
$reportFileName = 'dupfinder.html'
$dupFindLocation = 'C:\dev\tools\ReSharperCliTools\dupfinder.exe'
if (-not (Test-Path $dupFindLocation)) {
Write-Host "dupfinder.exe not found in path $dupFindLocation"
Write-Host "Download tools from https://www.jetbrains.com/help/resharper/2017.1/ReSharper_Command_Line_Tools.html"
exit
}
$xsl = '<?xml version="1.0" encoding="utf-8"?>
@badmotorfinger
badmotorfinger / Enable-CSharp-WarningsAsErrors.ps1
Last active June 15, 2017 06:44
Recursively processes all csproj files in a directory and adds/updates a node for both Release and Debug configurations to treat the specified C# warnings as errors
# Warnings to treat as errors
# See end of script for descriptions
$cSharpWarnings = '162,168,169,219,1717,0067,649,618,183,184,1060,1058,809,672,612,1522,465,1998'
$formatting = ',1570,1574'
$interopWarnings = ',626,684,824'
$casWarnings = ',688'
$warnings = $cSharpWarnings + $formatting + $interopWarnings + $casWarnings
Get-ChildItem *.csproj -Recurse | % {
// Only let input Observable fire every 'n' seconds at most
// but unlike Throttle, items fire immediately when they aren't
// rate-limited.
public IObservable<T> RateLimit<T>(this IObservable<T> This, TimeSpan interval, IScheduler scheduler)
{
var slot = default(IObservable<Unit>);
var input = This.Publish().RefCount();
return input.Window(input, _ => {
if (slot != null) return slot;
@badmotorfinger
badmotorfinger / gist:5972009
Last active December 19, 2015 14:49
A LINQ extension method which eagerly finds an item or items with the lowest value in the collection.
[Test]
public void TestEmpty()
{
CollectionAssert.AreEqual(
new int[0].MinsBy(i => i),
new int[0]);
}
[Test]
public void TestPreserveOrderAndValue()
public static class TestHelpers
{
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue)
{
ShouldEqualWithDiff(actualValue, expectedValue, DiffStyle.Full, Console.Out);
}
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle)
{
ShouldEqualWithDiff(actualValue, expectedValue, diffStyle, Console.Out);
@badmotorfinger
badmotorfinger / TryParseFunc.cs
Created July 18, 2012 03:38
Generic function to cast a string value to another value using .NET's built in TryParse methods.
// Usage
var intResult = Parse("1", 0, int.TryParse); // Returns 1
var dateResult = Parse("asdf", DateTime.Now, DateTime.TryParse); // Returns 18/07/2012 1:39:06 PM
delegate TParsedValue ParseFunc<T, U, TParsedValue>(T input, out U output);
static V Parse<T, V>(T valueToBeParsed, V defaultVal, ParseFunc<T, V, bool> dele) {
V result;