Skip to content

Instantly share code, notes, and snippets.

@SeeminglyScience
SeeminglyScience / Microsoft.VSCode_profile.ps1
Last active July 15, 2023 02:43
Snippet for enabling cross module "Find All References", "Go to Definition" and intellisense in VSCode PowerShell
if ($psEditor) {
# Don't reference any files whose FullName match this regex.
${Exclude Files Regex} = '\\Release\\|\\\.vscode\\|build.*\.ps1|debugHarness\.ps1|\.psd1'
# Get the PowerShellEditorServices assemblies.
${editor services assemblies} = [System.AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object Location -Match 'PowerShell.EditorServices.*.dll' |
ForEach-Object -MemberName Location
# Add some C# that essentially lets us add a hook to the event that is called when VSCode opens a file.
Add-Type -Language CSharp -ReferencedAssemblies ${editor services assemblies} -WarningAction SilentlyContinue -TypeDefinition @'

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@SeeminglyScience
SeeminglyScience / PSClassCmdlet.psm1
Last active January 12, 2024 11:00
Cmdlet example using only PowerShell.
# https://seeminglyscience.github.io/powershell/2017/04/13/cmdlet-creation-with-powershell
using namespace System.Management.Automation
using namespace System.Reflection
[Cmdlet([VerbsDiagnostic]::Test, 'Cmdlet')]
class TestCmdletCommand : PSCmdlet {
[Parameter(ValueFromPipeline)]
[object]
$InputObject;
@SeeminglyScience
SeeminglyScience / ForceClassExportExample.ps1
Created April 17, 2017 02:07
Proof of concept for forcing external type definitions to export in a module.
using namespace System.Management.Automation.Language
using namespace System.Collections.Generic
using namespace System.Reflection
# The current contents of the psm1 file would go here, including dot sourcing the class definition
# files normally and exporting module members.
# The rest can most likely be loaded into a function but I haven't tested it yet. It could also
# use some cleaning up.
$usingStatements = [List[UsingStatementAst]]::new()
@SeeminglyScience
SeeminglyScience / AssertTypeRunspace.ps1
Created June 2, 2017 19:55
Force PS classes to be marshaled to a specific runspace.
<#
Simple script that forces all static methods of types made
with PowerShell to be bound to the current runspace. If you
are getting NullReference exceptions every now and then, this
might fix it.
I put this in my profile as a temporary workaround, I do not
recommend adding this to a module. It uses a large amount of
reflection and could be unpredictable.
#>
@SeeminglyScience
SeeminglyScience / clr-powershell.ts
Created June 16, 2017 02:22
PoC draft of using node-clr to work with PowerShell. Requires node modules "clr" and "events".
import { EventEmitter } from 'events'
var clr = require('clr');
var namespaces = clr.init({ assemblies: [ 'System.Management.Automation' ], global: false });
export function forEachClr (collection: any, callback: (item: any) => any) {
let enumerator = collection.GetEnumerator();
while (enumerator.MoveNext()) callback(enumerator.Current)
}
# This should be included in SQL server if you have it installed, but I don't on this machine.
$package = Find-Package Microsoft.SqlServer.TransactSql.ScriptDom -Source https://www.nuget.org/api/v2
$package | Save-Package -Path $PWD
$fileBaseName = $package.Name + '.' + $package.Version
Rename-Item "$fileBaseName.nupkg" "$fileBaseName.zip"
Expand-Archive "$fileBaseName.zip"
Add-Type -Path $fileBaseName\lib\net40\Microsoft.SqlServer.TransactSql.ScriptDom.dll
# Example from https://docs.microsoft.com/en-us/sql/t-sql/queries/select-examples-transact-sql
$tSqlSample = @'
@SeeminglyScience
SeeminglyScience / PSReadlineCommandPalette.ps1
Last active July 31, 2017 02:52
POC VSCode style Command Palette for PSReadline
#requires -Version 5.1 -Module @{ModuleName = 'PSReadline'; RequiredVersion=1.2}
function Invoke-CommandPalette {
param(
[System.Nullable[System.ConsoleKeyInfo]] $key,
[object] $arg
)
end {
# Save the current buffer, cursor position, selection range and selection command count to
# be restored after the command is found.
$typeDefinition = @'
using System;
using System.Management.Automation;
using System.Reflection;
namespace PSStateTree.Commands
{
[OutputType(typeof(StateTreeInfo))]
[Cmdlet(VerbsCommon.Show, "PSStateTree")]
public class ShowPSStateTreeCommand : PSCmdlet
@SeeminglyScience
SeeminglyScience / New-PowerShellApp.ps1
Created October 14, 2017 18:29
Very simple example of creating a silent executable that runs a PowerShell script
function New-PowerShellApp {
[CmdletBinding()]
param([string]$FilePath, [string]$Script)
end {
$Script = $Script -replace '"', '""'
Add-Type -OutputType WindowsApplication -OutputAssembly $FilePath -TypeDefinition @"
using System.Management.Automation;
namespace SilentPowerShell