Skip to content

Instantly share code, notes, and snippets.

@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 = "\" }
) {
@nohwnd
nohwnd / list-vs-arr.ps1
Created January 25, 2019 19:11
list vs arr
$arr = 1..100
$repe = 1..100
Measure-Command {
foreach ($i in $repe) {
$l = New-Object 'System.Collections.Generic.List[Object]'
foreach ($ii in $arr) {
$l.Add($ii)
}
}
$l.count -eq $arr.count
@nohwnd
nohwnd / discovery of expensive test.ps1
Last active January 20, 2019 07:46
Discovery in Pester v5 does not run everything twice
Get-Module Pester | Remove-Module
Import-Module ./Pester.psd1
$sb = {
Add-Dependency {
Start-Sleep -Seconds 10
}
Describe "integration test" {