Skip to content

Instantly share code, notes, and snippets.

Avatar

Bartek Bielawski bielawb

View GitHub Profile
@bielawb
bielawb / invoke-insult.ps1
Created Nov 17, 2017
If you want your PowerShell to insult you when you run wrong command...
View invoke-insult.ps1
$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 / GitAliasesHelper.ps1
Created Nov 10, 2017
Snippet that can be added to the profile to turn git aliases into PowerShell commands. Note: this solution doesn't support parameters.
View GitAliasesHelper.ps1
$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 Dec 30, 2016
Just an attempt to get info about classes defined by a given module.
View Get-ModuleDefinedClass.ps1
[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 Dec 30, 2016
Work-around for the fact that `using module` is not very flexible.
View New-ModuleClassInstance.ps1
param (
[string]$Path,
[string]$ClassName
)
$scriptBody = @'
using module {0}
'@ -f $Path
$script = [scriptblock]::Create($scriptBody)
@bielawb
bielawb / CompleteShould.ps1
Created Nov 29, 2016
TabExpansion for Should
View CompleteShould.ps1
Register-ArgumentCompleter -Native -CommandName Should -ScriptBlock {
param ($WordToComplete, $CommandAst)
$options = @(
'Be'
'BeExactly'
'BeLike'
'BeGreaterThan'
'BeLessThan'
'Contain'
'Match'
@bielawb
bielawb / ValidateSet.Tests.ps1
Created Nov 1, 2016
Pester test for ValidateSet
View ValidateSet.Tests.ps1
function foo {
param (
[ValidateSet(
'this',
'that'
)]
[String]$bar
)
}
@bielawb
bielawb / FindADObject.psm1
Created Oct 31, 2016
Module that shows how hard to read can ArgumentCompleter become when query is complex.
View FindADObject.psm1
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 Oct 31, 2016
Module that uses WMF5 completion for skeleton New-JiraIssue
View JIRA.psm1
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 Oct 31, 2016
Module that uses WMF5 completion for vmconnect wrapper.
View ShowVM.psm1
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*") {
@bielawb
bielawb / ComputerName.Completer.ps1
Created Sep 28, 2016
Using TabExpansionPlusPlus to tab-complete CName -> HostName
View ComputerName.Completer.ps1
Register-ArgumentCompleter -ParameterName ComputerName -ScriptBlock {
param(
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameter
)
$record = [System.Net.Dns]::Resolve($wordToComplete)
if ($record) {