Skip to content

Instantly share code, notes, and snippets.

@rjmholt
rjmholt / CloseHandler.cs
Created October 6, 2021 17:19
Window close handler (for PowerShell)
public class CloseHandler : IModuleAssemblyInitializer
{
public void OnImport()
{
SetConsoleCtrlHandler(OnControlCode, add: true);
}
private static bool OnControlCode(CtrlType ctrlType)
{
switch (ctrlType)
@rjmholt
rjmholt / $Instructions.md
Last active September 27, 2021 17:07
PSSA Custom Rules

Custom rule DLLs in PSScriptAnalyzer

This gist shows an example of how to create custom rules with DLLs in PSScriptAnalyzer.

Requirements

There are a few obstacles to surmount before you can make custom rule DLLs work in PSScriptAnalyzer:

  • The latest PSScriptAnalyzer release doesn't support custom rule DLLs. Instead you need the changes in PSScriptAnalyzer #1718.
@rjmholt
rjmholt / AvoidMultipleTypeAttributes.cs
Created August 17, 2021 18:06
AvoidMultipleTypeAttributes
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// UseConsistentCasing: Check if cmdlet is cased correctly.
/// </summary>
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
public sealed class AvoidMultipleTypeAttributesRule : IScriptRule
{
@rjmholt
rjmholt / PrefixTrieNode.cs
Created June 10, 2021 01:21
Trie implementations in C#
public class PrefixTrieNode<TValue> where TValue : class
{
private readonly StringComparison _strCmp = StringComparison.OrdinalIgnoreCase;
public PrefixTrieNode(string prefix)
{
Prefix = prefix;
Children = new SortedList<string, PrefixTrieNode<TValue>>(StringComparer.FromComparison(_strCmp));
}
@rjmholt
rjmholt / conditionalArray.ps1
Created April 19, 2021 17:51
Conditional array example
# Example 1
$statArgs = @(
'-c'
if ($IsMacOS) { '%A' } else { '%a' }
'/etc/passwd'
)
# Also note that this is an example of array splatting with a native command
/bin/stat @statArgs
@rjmholt
rjmholt / ExampleProcess.cs
Last active March 13, 2021 20:48
Close a process gracefully in .NET
using System;
namespace example
{
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.ProcessExit += OnExit;
Console.WriteLine($"PID: {System.Diagnostics.Process.GetCurrentProcess().Id}");
@rjmholt
rjmholt / powershellAPI.md
Last active November 26, 2021 11:49
Using the PowerShell API

Using the PowerShell .NET API

There are a number cases where it's desirable to call PowerShell from another .NET language, particularly from C#. The two main classes of scenario here are:

  • Running PowerShell code or reusing PowerShell commands from a cmdlet or module.
  • Using PowerShell from a standalone .NET application, usually by hosting a fresh instance of PowerShell

Both use cases have a largely similar usage pattern,

@rjmholt
rjmholt / PowerShellTaskRunner.cs
Last active June 16, 2022 05:36
Examples of how to run PowerShell with a runspace pool and async Tasks
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Threading.Tasks;
/// <summary>
@rjmholt
rjmholt / hiddencmdlet.cs
Last active December 7, 2020 21:38
Use a hidden/unexported binary cmdlet in C#
using System;
using System.Linq;
using System.Management.Automation;
// Notice no [Cmdlet] attribute, otherwise PowerShell will export this cmdlet
// However, if you use this with a manifest you can add the attribute and just omit it from the manifest
// (the DLL will export the cmdlet, but then its absence in the manifest will filter it out)
internal class HiddenCmdlet : Cmdlet
{
[Parameter]
@rjmholt
rjmholt / resume.json
Last active January 5, 2021 18:42
CV
{
"meta": {
"theme": "flat"
},
"basics": {
"name": "Rob Holt",
"label": "Software Engineer",
"email": "rjmholt@gmail.com",
"summary": "Software engineer and open-source maintainer. I like to build robust, systematic solutions to problems.",
"location": {