Skip to content

Instantly share code, notes, and snippets.

@quonic
Created August 28, 2023 16:05
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 quonic/b9b92b3c061f35dbea69942dd7d49541 to your computer and use it in GitHub Desktop.
Save quonic/b9b92b3c061f35dbea69942dd7d49541 to your computer and use it in GitHub Desktop.
Understanding GetNewClosure. Assuming you have Pester installed, but not required. Remove Should parts.
# No GetNewClosure - print $a as what ever it is before printing
& {
$a = 10
$myScript = {
$a
}
& $myScript
$a = 20
& $myScript
} | Should -Be @(10, 20)
# GetNewClosure - print $a as what it was at the time of creation
& {
$a = 30
$myScript = {
$a
}.GetNewClosure()
& $myScript
$a = 40
& $myScript
} | Should -Be @(30, 30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment