Skip to content

Instantly share code, notes, and snippets.

@rjmholt
rjmholt / CLCheckRule.cs
Created April 15, 2019 21:43
PSScriptAnalyzer rule to check Constrained Language Mode scripts
using System;
using System.Collections.Generic;
using System.Management.Automation.Language;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
public class AvoidConstrainedLanguageErrors : ConfigurableRule
{
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
@rjmholt
rjmholt / pwshPerfTest.ps1
Last active June 27, 2019 20:31
Performance test for pwsh -Login
param(
[int]$Iterations = 100,
[switch]$Clean
)
$ErrorActionPreference = 'Stop'
function Exec
{
param(
@rjmholt
rjmholt / GetProcArgv.cs
Created July 11, 2019 03:13
Get current process invocation name on macOS
using System;
using System.Runtime.InteropServices;
namespace CSharpPlayground
{
public static class GetProcArgv
{
private const int CTL_KERN = 1;
private const int KERN_ARGMAX = 8;
private const int KERN_PROCARGS2 = 49;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Reflection;
using System.Text;
namespace RobPsUtils
{
public abstract class PSObjectSerializer
@rjmholt
rjmholt / CellFindingVisitor.cs
Created August 11, 2020 16:47
PowerShell AST visitor to break up a file by comments
using System;
using System.Collections.Generic;
using System.Management.Automation.Language;
public class ScriptExtent : IScriptExtent
{
private readonly IScriptPosition _start;
private readonly IScriptPosition _end;
@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": {
@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 / 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 / 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 / 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}");