Skip to content

Instantly share code, notes, and snippets.

View santisq's full-sized avatar

Santiago Squarzon santisq

View GitHub Profile

Compile and load the type... this could be pre-compiled e.g. via dotnet CLI then the type loaded via Assembly.LoadFrom(...) or Add-Type or Import-Module if part of an actual PowerShell Module.

Once loaded you can Update-TypeData -TypeAdapter like shown in the example:

Add-Type -Path .\adapterExample.cs -WA 0 -IgnoreWarnings
Update-TypeData -TypeAdapter ([MyPropertyAdapter]) -TypeName ([MyCustomClass])
@santisq
santisq / brave-debloat-lin.sh
Last active May 25, 2026 15:19
Brave debloat
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "Requires sudo: 'sudo $0'."
exit 1
fi
sudo mkdir -p /etc/brave/policies/managed/ && \
sudo tee /etc/brave/policies/managed/brave_policy.json > /dev/null << 'EOF'
{
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Management.Automation;
using System.Threading.Tasks;
internal sealed class MemoryCounterSampleFactory(PerformanceCounter counter, ScriptBlock invocation)
: IDisposable
{
private const long Mb = 1_048_576;
@santisq
santisq / aes-cbc.cs
Last active March 29, 2026 13:03
AES encrypt / decrypt strings
using System;
using System.IO;
using System.Management.Automation;
using System.Security.Cryptography;
using System.Text;
[Cmdlet(VerbsDiagnostic.Test, "Encrypt")]
public sealed class EncryptAes : PSCmdlet
{
[Parameter(Mandatory = true, Position = 0)]
@santisq
santisq / CustomVariableProvider.cs
Last active March 20, 2026 20:01
A custom PowerShell variable provider implementation that evaluates script blocks on inspection
// Source - https://stackoverflow.com/a/79911456
// Posted by Santiago Squarzon, modified by community. See post 'Timeline' for change history
// Retrieved 2026-03-20, License - CC BY-SA 4.0
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Provider;
# Requires module ClassExplorer
$method = [System.Management.Automation.LanguagePrimitives] | fime -f FigureConversion -ParameterCount 3
# Rank: NullToValue
$flag = $null
$converter = $method.Invoke($null, @($null, [int], $flag))
$converter.Invoke($null, [int], $true, $null, [cultureinfo]::InvariantCulture, $null)
# Rank: NullToRef
$flag = $null
@santisq
santisq / keepalive.cs
Last active February 27, 2026 22:54
SetThreadExecutionState... keeps your powershell script running without going to sleep
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
public static class Native
{
private static EXECUTION_STATE? s_prevState;
private const uint INPUT_KEYBOARD = 1;
private const uint KEYEVENTF_KEYUP = 0x0002;
@santisq
santisq / CsvUtils.cs
Last active May 22, 2026 14:13
Adapted `Import-Csv` source to output `IEnumerable<T>`
// Adapted from PowerShell's Import-Csv implementation to be used in standalone C# projects.
// https://github.com/PowerShell/PowerShell/blob/4838a8d5f6c95339d4b44c8b402165e69e6ff929/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq.Expressions;
using System.Management.Automation;
using System.Reflection;
using System.Text;
@santisq
santisq / InvokeRetry.ps1
Created October 20, 2025 12:33
simple retry function
function Invoke-Retry {
[Alias('retry')]
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[scriptblock] $Scriptblock,
[Parameter()]
[scriptblock] $RetryHandler,
function Get-EffectiveAccess {
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidatePattern('(?:(CN=([^,]*)),)?(?:((?:(?:CN|OU)=[^,]+,?)+),)?((?:DC=[^,]+,?)+)$')]
[alias('DistinguishedName')]
[string] $Identity,
[parameter()]
[alias('Domain')]