Skip to content

Instantly share code, notes, and snippets.

@nohwnd
Created July 2, 2021 07:49
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 nohwnd/7344dfbb2eaa36ccbe7a7d8f9ce7e8c5 to your computer and use it in GitHub Desktop.
Save nohwnd/7344dfbb2eaa36ccbe7a7d8f9ce7e8c5 to your computer and use it in GitHub Desktop.
scriptblock local storage
# 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