Skip to content

Instantly share code, notes, and snippets.

@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;
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 / 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;
@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 / 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 / Get-PSEncoding.ps1
Created February 21, 2019 07:36
PowerShell BOM-less encoding
$badBytes = [byte[]]@(0xC3, 0x80)
$utf8Str = [System.Text.Encoding]::UTF8.GetString($badBytes)
$bytes = [System.Text.Encoding]::ASCII.GetBytes('Write-Output "') + [byte[]]@(0xC3, 0x80) + [byte[]]@(0x22)
$path = Join-Path ([System.IO.Path]::GetTempPath()) 'encodingtest.ps1'
try
{
@rjmholt
rjmholt / PsesHelpers.ps1
Created September 17, 2018 19:53
Helper functions deleted from a PSES PR that might come in handy later
function Enable-WritingToPath {
param([string]$Path)
# This is not a problem on Windows
if (-not ($IsMacOS -or $IsLinux)) {
return
}
Write-Verbose "Setting permissions on path: $Path"
@rjmholt
rjmholt / AuthenticationValidator.cs
Last active July 17, 2018 22:47
C# snippet for doing HMAC-based authentication
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol
{
@rjmholt
rjmholt / AclStuff.ps1
Last active July 17, 2018 22:46
Script for ACL-ing things to the current user in PowerShell
param(
[ValidateNotNullOrEmpty()]
[string]$KeyFileName,
[ValidateNotNullOrEmpty()]
[string]$KeyFileDir = $null
)
# Test if the given Windows Identity is the Built-in Administrator
function Test-IsAdministrator
{
@rjmholt
rjmholt / PssaRunspacePoolCreate.cs
Created June 5, 2018 16:29
Better PSScriptAnalyzer runspace pool creation
private static RunspacePool CreatePssaRunspacePool()
{
// Use public but unfriendly APIs to import a PSScriptAnalyzer module with the needed minimum version
var pssaModuleSpec = new ModuleSpecification(new Hashtable()
{
{ "ModuleName", PSSA_MODULE_NAME },
{ "ModuleVersion", s_pssaMinimumVersion }
});