Skip to content

Instantly share code, notes, and snippets.

@nohwnd
nohwnd / browser.ps1
Created January 16, 2019 09:53
Refreshing browser from other runspace
$syncHash = [hashtable]::Synchronized(@{
Browser = $null
Window = $null
})
$psCmd = [powershell]::Create()
$newRunspace = [RunspaceFactory]::CreateRunspace()
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable('syncHash', $syncHash)
@nohwnd
nohwnd / dependency.ps1
Created January 7, 2019 21:12
Controlled dot-sourcing
function k () {
write-host function k from file
}
$b = "from file"
@nohwnd
nohwnd / ItWithTag.ps1
Created January 7, 2019 18:02
Adding a filter to It via wrapper
function ItWithTag (
$Name,
$Body,
$Tag
) {
if ($null -eq $global:Tags -or 0 -eq $global:Tags.Length -or $global:Tags -contains $Tag) {
It $Name $Body
}
}
@nohwnd
nohwnd / transparent wrapper.ps1
Created January 7, 2019 11:21
Transparent wrapper using data object
# getting a piece of code from a user that we want to wrap,
# and execute, we often need to pass in additional parameters
# at least the script itself, and this poses a problem because
# the parameter name will conflict with the user script.
# (we want to add a wrapper for example to run two pieces of code
# in the same scope, without leaking variables into a parent scope)
# here for simplicity all the scriptblocks are running in the
# same session state, and we chose variable $A to pass the user
@nohwnd
nohwnd / file.ps1
Created January 6, 2019 09:38
Navigatable stack trace
function a () {
b
}
function b () {
c
}
function c () {
throw "error"
@nohwnd
nohwnd / deffer.ps1
Created December 21, 2018 09:16
Deffered evaluation for logs
# Logging verbose info to screen is useful for debugging
# but the info is hidden 99% of the time. Problem is that
# evaluating a string to get the verbose message will run
# even if we never actually print it, adding overhead to
# every run, not just runs that are verbose.
# to avoid this passing a script block that evaluates to the
# message string conditionally seems to work well
@nohwnd
nohwnd / examples.ps1
Last active December 20, 2018 21:38
p - a minimal implementation of Pester
# runs tests with summary
i {
b "Basic" {
t "Given a scriptblock with 1 test in it, it finds 1 test" {
$true
}
}
}
# runs tests without summary
@nohwnd
nohwnd / RetryAndFix.ps1
Created December 18, 2018 12:53
Apply a remedy after a Pester test fails
# Apply a remedy after a Pester test fails to fix the problem that caused
# the test to fails (eg. all inactive AD users should be disabled,
# so a remedy is to disable all inactive users)
function RetryAndFix ([scriptBlock]$Test, [scriptBlock]$Fix, $Wait) {
try {
& $Test
}
catch {
@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 () {