Created
May 31, 2019 09:39
-
-
Save nohwnd/aeb63b272c03ab084560083ebbc75d73 to your computer and use it in GitHub Desktop.
Define variable in caller scope
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
# Sets variable $a = 1000 in the local scope from which it was called from, like $matches in regex | |
Get-Module m | Remove-Module | |
New-Module m { | |
function f { | |
[CmdletBinding()] | |
param() | |
$sb = [ScriptBlock]::Create('param($Value); $a = $Value') | |
$ExecutionContext.InvokeCommand.InvokeScript($PSCmdlet.SessionState, $sb, @(1000)) | |
} | |
} | Import-MOdule | |
Get-Variable a -ErrorAction Ignore | Remove-Variable | |
f | |
"a: $a" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment