Skip to content

Instantly share code, notes, and snippets.

@jonforums
Last active December 13, 2015 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonforums/4956878 to your computer and use it in GitHub Desktop.
Save jonforums/4956878 to your computer and use it in GitHub Desktop.
toying with powershell scriptblocks
#require -version 2.0
function invoker($data, $fcn) { $fcn.Invoke($data) }
$sport = 'ski'
$lambda = { param ($who) write-host "Hello $who, do you $sport" }
# direct call with mixed array of mixed values
& $lambda 'Foo', 7
# I'm a `ScriptBlock` with an `Invoke` method
$lambda.Invoke('Buddy')
# yikes, stop...this...insanity...now :(
Invoke-Command -scriptblock $lambda -argumentlist 'PowerShell 3.0'
# have someone else do the dirty work
invoker 'Lazy PowerShell' $lambda
PS C:\Users\Jon> .\scriptblock_tricks.ps1
Hello Foo 7, do you ski
Hello Buddy, do you ski
Hello PowerShell 3.0, do you ski
Hello Lazy PowerShell, do you ski
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment