Skip to content

Instantly share code, notes, and snippets.

View bielawb's full-sized avatar

Bartek Bielawski bielawb

View GitHub Profile
@bielawb
bielawb / invoke-insult.ps1
Created November 17, 2017 20:36
If you want your PowerShell to insult you when you run wrong command...
$ExecutionContext.InvokeCommand.CommandNotFoundAction = {
param ($name, $eventArgs)
@(
"Boooo!"
"Don't you know anything?"
"RTFM!"
"Hahaha, n00b!"
"Wow! That was impressively wrong!"
"He said: $name :D"
"What are you doing??"
@bielawb
bielawb / FixDolarUnderscoreUnderscore.ps1
Created September 7, 2022 09:54
Fix $__ in PS7 with PSReadLine/ replacing it with Get-History call.
Set-PSReadlineKeyHandler -Chord Spacebar -ScriptBlock {
param ($key, $arg)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState(
[ref]$line, [ref]$cursor
)
if ($cursor -lt 3) {
return
@bielawb
bielawb / ValidateSet.Tests.ps1
Created November 1, 2016 19:10
Pester test for ValidateSet
function foo {
param (
[ValidateSet(
'this',
'that'
)]
[String]$bar
)
}
@bielawb
bielawb / CompleteShould.ps1
Created November 29, 2016 18:44
TabExpansion for Should
Register-ArgumentCompleter -Native -CommandName Should -ScriptBlock {
param ($WordToComplete, $CommandAst)
$options = @(
'Be'
'BeExactly'
'BeLike'
'BeGreaterThan'
'BeLessThan'
'Contain'
'Match'
@bielawb
bielawb / GitAliasesHelper.ps1
Created November 10, 2017 09:45
Snippet that can be added to the profile to turn git aliases into PowerShell commands. Note: this solution doesn't support parameters.
$gitAliases = (git config --global -l).Where{ $_ -match '^alias\.'}.ForEach{$_ -replace '^alias\.(\w+).*', '$1'}
$ExecutionContext.InvokeCommand.CommandNotFoundAction = {
param ($name, $eventArgs)
if ($name -in $gitAliases) {
$alias = $name
} elseif ($aliases = $gitAliases -match [regex]::Escape($name)) {
$alias = $aliases | Sort-Object -Property Length | Select-Object -First 1
}
@bielawb
bielawb / Get-ModuleDefinedClass.ps1
Last active December 30, 2016 08:33
Just an attempt to get info about classes defined by a given module.
[CmdletBinding()]
[OutputType([hashtable])]
param (
[string]$Path,
[string]$ClassName = '*'
)
$moduleBase = Split-Path -Path $Path -Parent
Write-Verbose "Module name - $fileName"
@bielawb
bielawb / New-ModuleClassInstance.ps1
Created December 30, 2016 07:27
Work-around for the fact that `using module` is not very flexible.
param (
[string]$Path,
[string]$ClassName
)
$scriptBody = @'
using module {0}
'@ -f $Path
$script = [scriptblock]::Create($scriptBody)
@bielawb
bielawb / FindADObject.psm1
Created October 31, 2016 23:21
Module that shows how hard to read can ArgumentCompleter become when query is complex.
function Find-ADObject {
<#
.Synopsis
Function to search Active Directory using specified LDAP filter.
.Description
Function uses selected LDAP filter to search Active Directory.
It doesn't have any external dependencies and is using ADSISearcher class.
@bielawb
bielawb / JIRA.psm1
Created October 31, 2016 23:09
Module that uses WMF5 completion for skeleton New-JiraIssue
function Get-LoginCompletion {
param(
[string]$commandName,
[string]$parameterName,
[string]$wordToComplete,
[System.Management.Automation.Language.CommandAst]$commandAst,
[System.Collections.IDictionary]$fakeBoundParameters
)
([ADSISearcher]"(&(objectClass=user)(sAMAccountName=$wordToComplete*))").FindAll() |
@bielawb
bielawb / ShowVM.psm1
Created October 31, 2016 22:48
Module that uses WMF5 completion for vmconnect wrapper.
class DopelnijVMke : System.Management.Automation.IArgumentCompleter {
[System.Collections.Generic.IEnumerable[System.Management.Automation.CompletionResult]] CompleteArgument(
[string]$commandName,
[string]$parameterName,
[string]$wordToComplete,
[System.Management.Automation.Language.CommandAst]$commandAst,
[System.Collections.IDictionary]$fakeBoundParameters
) {
return [System.Management.Automation.CompletionResult[]]@(
foreach ($vm in Get-VM -Name "$wordToComplete*") {