Created
July 2, 2021 07:49
-
-
Save nohwnd/7344dfbb2eaa36ccbe7a7d8f9ce7e8c5 to your computer and use it in GitHub Desktop.
scriptblock local storage
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
# create a scriptblock that has scriptblock local data attached (data common to all invocation of the scriptblock) | |
# but is still bound to the same session state, this is differnt from closure, because closures are their own modules | |
$sb = { | |
$location | |
$self = $MyInvocation.MyCommand.ScriptBlock | |
($self.___data.Count++) | |
} | |
$sb.PSObject.Properties.Add([System.Management.Automation.PSNoteProperty]::new("___data", @{ Count = 1})) | |
$location = "script" | |
& $sb | |
& $sb | |
& $sb | |
& $sb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment