Skip to content

Instantly share code, notes, and snippets.

View KirkMunro's full-sized avatar
💤
Busy working on other projects

Kirk Munro KirkMunro

💤
Busy working on other projects
View GitHub Profile
@KirkMunro
KirkMunro / Find-PSCmdletSource.cs
Created November 2, 2018 19:40
Find the open source code for a cmdlet that ships as part of PowerShell Core
function Find-PSCmdletSource {
[CmdletBinding()]
[OutputType([string])]
[Alias('oss')]
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[Alias('Name')]
[string[]]
$CmdletName,
@KirkMunro
KirkMunro / 1. Before PR #8057
Last active October 17, 2018 11:21
Invoking multiple PowerShell commands one at a time from C#
var sessionState = InitialSessionState.CreateDefault();
sessionState.ImportPSModule(new string[] { "AzureRM.Profile" });
using (var psRunspace = RunspaceFactory.CreateRunspace(sessionState))
{
using (var ps = PowerShell.Create())
{
// This is not intuitive and an unnecessary annoyance
ps.Runspace = psRunspace;
@KirkMunro
KirkMunro / Compare-ObjectEquivalence.ps1
Created June 6, 2017 15:42
A function that facilitates comparison of serialized objects with their deserialized or selected equivalent
function Compare-ObjectEquivalence {
[CmdletBinding(DefaultParameterSetName='Deserialized')]
param(
[Parameter(Position=0, Mandatory=$true)]
[ValidateNotNull()]
[System.Object]
$OriginalObject,
[Parameter(Position=1, Mandatory=$true, ParameterSetName='Deserialized')]
[ValidateNotNull()]
#region Fix important *nix commands.
# PowerShell comes with some predefined aliases built-in that are designed to
# ease the transition from *nix to PowerShell. While this is well-intentioned,
# it hides the true power that is available in these *nix commands, and it
# makes it much more difficult to compare and contrast between native commands
# in PowerShell and their *nix counterparts. This block of code removes the
# predefined aliases that would otherwise hide these important *nix commands.
foreach ($nixCommand in @('cat','cp','curl','diff','echo','kill','ls','man','mount','mv','ps','pwd','rm','sleep','tee','type','wget')) {
@KirkMunro
KirkMunro / verbs.ps1
Last active July 19, 2018 07:25
Show a custom sorted grid of all PowerShell verbs in an easy to read format
<#
.SYNOPSIS
Show all verbs in an easy-to-read grid.
.DESCRIPTION
Show all verbs in a grid, with custom sorting applied so that we can quickly scan each column and find verbs more easily.
This command was designed for ad-hoc use only. To get the most value out of this command, place it in your profile script. It outputs format data which is not easily used in automated commands.
#>
function verbs {
# Get the verbs in a sorted list
@KirkMunro
KirkMunro / BetterScriptDiscovery prototype
Last active February 19, 2016 19:47
A prototype module that adds $env:PSScriptPath support to Windows PowerShell so that scripts can be invoked by name in well known locations, but only after the command name resolver looks for commands with the same name in modules first (i.e. modules have higher priority than scripts in this solution)
New-Module -Name BetterScriptDiscovery -ScriptBlock {
# First, let's make sure we have a PSScriptPath environment variable that initializes
# the same way that the PSModulePath environment variable does
$currentUserScriptsPath = [System.IO.Path]::Combine([System.Environment]::GetFolderPath('MyDocuments', [System.Environment+SpecialFolderOption]::DoNotVerify), 'WindowsPowerShell', 'Scripts')
$allUserScriptsPath = [System.IO.Path]::Combine([System.Environment]::GetFolderPath('ProgramFiles', [System.Environment+SpecialFolderOption]::DoNotVerify), 'WindowsPowerShell', 'Scripts')
$userPsScriptPath = [System.Environment]::GetEnvironmentVariable('PSScriptPath', [System.EnvironmentVariableTarget]::User)
$processPsScriptPath = [System.Environment]::GetEnvironmentVariable('PSScriptPath')
if ($processPsScriptPath -eq $null) {
$processPsScriptPath = ''
}
@KirkMunro
KirkMunro / PowerShell.Signed.gitattributes
Last active February 15, 2023 10:24
.gitattributes file contents for PowerShell script or manifest modules (signed or unsigned) and binary modules
# Treat all files in this project as binary. This enables atomic
# checkins (no merges, these are signed files) and it preserves
# CRLF line endings
* binary
@KirkMunro
KirkMunro / HackingCommandHistory.ps1
Last active August 29, 2015 14:07
Hacking PowerShell command history
# IMPORTANT NOTE: This hack has evolved, and HistoryPx is now a full PowerShell module
# that is hosted on GitHub and can be found here: github.com/KirkMunro/HistoryPx
# First set up some interesting hacks
New-Module -Name HistoryPx -ScriptBlock {
$PSModule = $ExecutionContext.SessionState.Module
$global:__ = $null
$global:MaximumDetailedHistoryCount = 50
$global:PSDefaultParameterValues['Out-Default:OutVariable'] = 'global:__'
$commandHistory = [ordered]@{}
[CmdletBinding()]
[OutputType([System.Management.Automation.PSModuleInfo])]
param(
# The name of the module to install from GitHub.
[Parameter(Position=0, Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[System.String[]]
$ModuleName,
# The scope from which the module should be discoverable.
@KirkMunro
KirkMunro / .Start-ScsmPxIntroduction.ps1
Last active August 29, 2015 13:57
A brief introduction to some of the capabilities provided by the ScsmPx module.
# Get all commands included in the ScsmPx module
Get-ScsmPxCommand
# Get all commands included in the SCSM native modules
Get-SCSMCommand