Skip to content

Instantly share code, notes, and snippets.

@nohwnd
nohwnd / example.ps1
Created December 15, 2018 22:59
Visible scopes in Pester
Get-Module Pester, A, B | Remove-Module
Import-Module C:\Projects\Pester_main\Pester.psd1
New-Module A { } | Import-Module
New-Module B {
function Greet ($Name) { Hello -Name $Name }
function Hello ($Name) { "Hello, $Name" }
Export-ModuleMember -Function WrapWrapWrap, Greet
} | Import-Module
@nohwnd
nohwnd / scopechecks.ps1
Created December 12, 2018 15:54
Shared state between DSL keywords in Pester
# in module scope
$script:pester
function describe () {
$script:pester.EnterBlock("describe")
}
function it () {
@nohwnd
nohwnd / wrappersb.ps1
Last active December 8, 2018 14:22
Make wrapper scriptblock transparent to avoid naming conflicts
$p1 = 10
$p2 = 20
$f = {
"-$p1- -$p2-"
}
& {
param($p1)
# outputs the scriptblock, instead of 10
# we have a naming conflict of names
@nohwnd
nohwnd / Invoke-WithContext.ps1
Last active December 8, 2018 14:14
Invoking scriptblocks with context
# InvokeWith context is useful when we have something like
# ```
# $num = 1
# 1,2,3 | Assert-All -filter { $num -eq $_ }
# ```
# Where Assert-All is defined in a module where we invoke the filter script. The script needs to
# stay bounded to the original scope to keep $name resolvable, but we also need to push the
# $_ variable into the script block when invoking it.
@nohwnd
nohwnd / options.ps1
Created December 7, 2018 11:49
Leaking parent scope fools my tests
function A {
[CmdletBinding()]
param()
if ($null -eq $Option) { throw }
}
Describe "A" {
it "works" {
$option = "abc"
@nohwnd
nohwnd / locala.ps1
Created December 7, 2018 11:36
Prevent parent scope variables from leaking into function
# variable in parent scope
$a = 1
function f1 ($a) {
# $a is shadowed by the parameter result is --
"-$a-"
}
f1
@nohwnd
nohwnd / SetAllUsersRegistryFromRegFile.ps1
Created November 29, 2018 11:05
Sets registry keys to all users present on the computer, including current and default user
#requires -RunAsAdministrator
param (
[Parameter(Mandatory)]
$Path)
$ErrorActionPreference = 'stop'
if (-not (Test-Path $Path)) {throw "Path $Path does not exist!" }
# import it for current user
@nohwnd
nohwnd / replace.ps1
Created November 28, 2018 18:02
replace properties on psobject
$MyObject = [PSCustomObject]@{ Value = 15; Value2 = 13; Value3 = "j" }
$Fields = @{
Value = "special"
Value2 = "Different"
}
$hashtable = @{}
foreach ($property in $MyObject.PSObject.Properties) {
if ($Fields.ContainsKey($property.Name))
@nohwnd
nohwnd / RenameFile.ps1
Created November 20, 2018 17:52
Rename file with scriptblock for newname
"a" | Set-Content "c:\temp\abc.txt" -Force
Remove-Item "c:\temp\bbc.txt"
Get-Item "C:\temp\abc.txt" |
Rename-Item -NewName { $_.Name -replace "a","b"}
Get-Content "c:\temp\bbc.txt"
@nohwnd
nohwnd / helptest.ps1
Created September 30, 2018 08:43
Automatic example testing
# command to be tested
$commandName = 'Get-Command'
# get all examples from the help
$examples = Get-Help $commandName -Examples
# make a describe block that will contain tests for this
Describe "Examples from $commandName" {
$examples.Examples.Example | foreach {
# examples have different format,
# at least the ones I used that MS provided