Last active
December 8, 2018 14:22
-
-
Save nohwnd/49154bbc788e032b8275797bf24f60d9 to your computer and use it in GitHub Desktop.
Make wrapper scriptblock transparent to avoid naming conflicts
This file contains hidden or 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
| $p1 = 10 | |
| $p2 = 20 | |
| $f = { | |
| "-$p1- -$p2-" | |
| } | |
| & { | |
| param($p1) | |
| # outputs the scriptblock, instead of 10 | |
| # we have a naming conflict of names | |
| & $p1 | |
| } $f | |
| & { | |
| param($p1) | |
| # outputs -10- -20- correctly | |
| & $($p1; Remove-Variable -Name p1 -Scope Local) | |
| # bcs it returns the scriptblock before deleting the | |
| # variable but, runs it after deleting the variable | |
| # effectively making the wrapping scriptblock | |
| # transparent | |
| } $f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment