Skip to content

Instantly share code, notes, and snippets.

@nohwnd
nohwnd / dot.ps1
Last active June 27, 2019 15:00
Dot sourcing
Import-Module Pester -MaximumVersion 4.9.9
# use the fact that closures are dynamic modules with
# their own session state, and that scriptblocks are bound
# to the session state that created them, that makes
# the call to `dot` transparent, because the scope
# that the function creates is in a different sessionstate
# and so the `$ScriptBlock` still dot-sources directly into
# the calling session state, just like with the naked `.`
$function:dot = {
@nohwnd
nohwnd / define-variable-in-caller-scope.ps1
Created May 31, 2019 09:39
Define variable in caller scope
# Sets variable $a = 1000 in the local scope from which it was called from, like $matches in regex
Get-Module m | Remove-Module
New-Module m {
function f {
[CmdletBinding()]
param()
$sb = [ScriptBlock]::Create('param($Value); $a = $Value')
$ExecutionContext.InvokeCommand.InvokeScript($PSCmdlet.SessionState, $sb, @(1000))
}
@nohwnd
nohwnd / wpf.ps1
Last active October 9, 2021 20:01
Weird WPF binding in PowerShell
# This example shows three versions of behavior, in the first the data binding only
# picks up the property that is defined on the type, in the second it picks up ever
# powershell adapted property Name (alias property), and in the third it picks up
# all properties even though they are all powershell properties and not on the PSObjectType
# is there some special handling for PSObject regarding WPF in Windows PowerShell?
Add-Type -AssemblyName PresentationFramework
[string]$xaml = @"
@nohwnd
nohwnd / sharing-test-data.ps1
Created March 9, 2019 07:31
Share data among tests safely
# GOOD:
# shares data among tests without polluting
# the script scope
Describe "I share data" {
$container = @{ Value = 1 }
It "I give data" {
$container.Value = 5
}
@nohwnd
nohwnd / Import-Script.psm1
Created February 17, 2019 19:04
Importing self-contained scripts. Demo for PSPowerHour.
function Import-Script {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[String] $Path,
[Hashtable] $Parameters = @{},
[Object[]] $Arguments = @(),
[String] $EntryPoint = 'Main'
)
@nohwnd
nohwnd / asciiart.ps1
Created February 13, 2019 18:52
Ascii art from api
function Get-Banner {
param (
[Parameter(Mandatory)]
[String] $Text
)
if (-not $fonts) {
$fonts = (Invoke-RestMethod -Method GET -Uri 'http://artii.herokuapp.com/fonts_list') -split "`n"
}
@nohwnd
nohwnd / AddOrUpdate.ps1
Last active February 10, 2019 16:12
Add or update to hashtable
function getOrUpdateValue {
[CmdletBinding()]
param(
$Hashtable,
$Key,
$DefaultValue
)
if ($Hashtable.ContainsKey($Key)) {
# do not enumerate so we get the same thing back
@nohwnd
nohwnd / mock-scope-0.ps1
Created January 29, 2019 22:12
Mock bug
function a () { }
describe "b" { mock a {}
context "b" {
describe "a" {
a
context "b" {
a
it "c" {
@nohwnd
nohwnd / get-errors.ps1
Created January 27, 2019 10:31
Collecting non terminating errors from scriptblock
get-module m,n,p | remove-module
# some tested code
New-Module m -ScriptBlock {
New-Module n -ScriptBlock {
function a { Write-Error "error abcd" }
} | Import-Module
function b {
@nohwnd
nohwnd / trimtrailingslashinpath.ps1
Created January 25, 2019 20:00
trims trailing slash in non-root paths
describe "path trimmer" {
it "does correct stuff" -TestCases @(
@{ Actual = "c:\"; Expected = "c:\" }
@{ Actual = "c:\abc"; Expected = "c:\abc" }
@{ Actual = "c:\abc\"; Expected = "c:\abc" }
@{ Actual = "\\abc\def"; Expected = "\\abc\def" }
@{ Actual = "\\abc\def\"; Expected = "\\abc\def" }
@{ Actual = "\\abc\def\gef\"; Expected = "\\abc\def\gef" }
@{ Actual = "\"; Expected = "\" }
) {