Last active
December 13, 2015 18:38
-
-
Save jonforums/4956878 to your computer and use it in GitHub Desktop.
toying with powershell scriptblocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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