Strange behavior with Powershell scriptblock variable scope and modules, any suggestions? Strange behavior with Powershell scriptblock variable scope and modules, any suggestions? powershell powershell

Strange behavior with Powershell scriptblock variable scope and modules, any suggestions?


I don't think this is considered to be a bug by the PowerShell team, but I can at least shed some light on how it works.

Any script block that's defined in a script or script module (in literal form, not dynamically created with something like [scriptblock]::Create()) is bound to the session state of that module (or to the "main" session state, if not executing inside a script module.) There is also information specific to the file that the script block came from, so things like breakpoints will work when the script block is invoked.

When you pass in such a script block as a parameter across script module boundaries, it is still bound to its original scope, even if you invoke it from inside the module.

In this specific case, the simplest solution is to create an unbound script block by calling [scriptblock]::Create() (passing in the text of the script block object that was passed in as a parameter):

. ([scriptblock]::Create($properties.ToString()))

However, keep in mind that there is potential for scope problems in the other direction, now. If that script block relies on being able to resolve variables or functions that were available in the original scope, but not from the module where you've invoked it, it will fail.

Since the intention of the $properties block appears to be to set variables and nothing else, I would probably pass in an IDictionary or Hashtable object instead of a script block. That way all of the execution takes place out in the caller's scope, and you get a simple, inert object to deal with inside the module, with no scope silliness to worry about:

function Test {    param(        [ValidateNotNull()]        [Parameter(Position=0,Mandatory=0)]        [System.Collections.IDictionary]$properties = @{}    )    # Setting the default    $message = "Hello, world!"    Write-Host "After setting defaults, message is: $message"    foreach ($dictionaryEntry in $properties.GetEnumerator())    {        Set-Variable -Scope Local -Name $dictionaryEntry.Key -Value $dictionaryEntry.Value    }    Write-Host "After importing properties, message is: $message"}

Caller file:

Import-Module .\repoCase.psm1Write-Host "Before execution - In global scope, message is: $message"Test -properties @{ Message = 'New Message' }Write-Host "After execution - In global scope, message is: $message"Remove-Module repoCase


I have been investigating this problem, which has come up in a project I am working on, and discovered three things:

  1. The issue is specific to modules.
    • If the code that invokes the scriptBlock is physically located anywhere within a .psm1 file, we see the behavior.
    • We also see the behavior if the code that invokes the scriptBlock is located in a separate script file (.ps1), if the scriptBlock was passed in from a module.
    • We do not see the behavior if the code that invoked the scriptBlock is located anywhere in a script file (.ps1), as long as the scriptBlock was not passed from a module.
  2. The scriptBlock will not necessarily execute in the global scope. Rather, it always appears to execute in whatever scope the module function was called from.
  3. The issue is not limited to the "." operator (dotsource). I have tested three different ways to invoke the scriptBlock: the "." operator, the "&" operator, and the scriptBlock object's invoke() method. In the latter two cases, the scriptBlock executes with the wrong parent scope. This can be investigated by trying to invoke, for example {set-variable -name "message" -scope 1 -value "From scriptBlock"}

I hope this sheds some more light on the problem, although I haven't quite gotten far enough to propose a workaround.

Does anyone still have PowerShell 1 installed? If so, it would be useful if you can check whether it displays the same behavior.

Here are the files for my test cases. To run them, create all four files in the same directory, and then execute "./all_tests.ps1" at the PowerShell ISE command line

script_toplevel.ps1

param($script_block)set-alias "wh" write-host$message = "Script message"wh "  Script message before:      '$message'". $script_blockwh "  Script message after:       '$message'"

script_infunction.ps1

param($script_block)set-alias "wh" write-hostfunction f {    param($script_block)    $message = "Function message"    wh "  Function message before:    '$message'"    . $script_block    wh "  Function message after:     '$message'"}$message = "Script message"wh "  Script message before:      '$message'"f -script_block $script_blockwh "  Script message after:       '$message'"

module.psm1

set-alias "wh" write-hostfunction simple_test_fun {    param($script_block)    $message = "ModFunction message"    wh "  ModFunction message before: '$message'"    . $script_block    wh "  ModFunction message after:  '$message'"}function ampersand_test_fun {    param($script_block)    $message = "ModFunction message"    wh "  ModFunction message before: '$message'"    & $script_block    wh "  ModFunction message after:  '$message'"}function method_test_fun {    param($script_block)    $message = "ModFunction message"    wh "  ModFunction message before: '$message'"    $script_block.invoke()    wh "  ModFunction message after:  '$message'"}function test_mod_to_script_toplevel {    param($script_block)    $message = "ModFunction message"    wh "  ModFunction message before: '$message'"    & .\script_toplevel.ps1 -script_block $script_block    wh "  ModFunction message after:  '$message'"}function test_mod_to_script_function {    param($script_block)    $message = "ModFunction message"    wh "  ModFunction message before: '$message'"    & .\script_infunction.ps1 -script_block $script_block    wh "  ModFunction message after:  '$message'"}export-modulemember -function "simple_test_fun", "test_mod_to_script_toplevel", "test_mod_to_script_function", "ampersand_test_fun", "method_test_fun"

all_tests.ps1

remove-module moduleimport-module .\module.psm1set-alias "wh" write-hostwh "Test 1:"wh "  No problem with . at script top level"wh "    ScriptBlock created at 'TopScript' scope"wh "    TopScript -amp-calls-> Script -dot-calls-> ScriptBlock:"whwh "  Expected behavior: Script message after:       'Script block message'"wh "  Problem behavior:  TopScript message after:    'Script block message'"whwh "Results:"$global:message = "Global message"$message = "Top script message"wh "  Global message before:      '$global:message'"wh "  TopScript message before:   '$message'"& .\script_toplevel.ps1 -script_block {$message = "Script block message"}wh "  TopScript message after:    '$message'"wh "  Global message after:       '$global:message'"whwh "Test 1 showed expected behavior"whwhwh "Test 2:"wh "  No problem with . inside function in script"wh "    ScriptBlock created at 'TopScript' scope"wh "    TopScript -amp-calls-> Script -calls-> Function -dot-calls-> ScriptBlock:"whwh "  Expected behavior: Function message after:     'Script block message'"wh "  Problem behavior:  TopScript message after:    'Script block message'"whwh "Results:"$global:message = "Global message"$message = "Top script message"wh "  Global message before:      '$global:message'"wh "  TopScript message before:   '$message'"& .\script_infunction.ps1 -script_block {$message = "Script block message"}wh "  TopScript message after:    '$message'"wh "  Global message after:       '$global:message'"whwh "Test 2 showed expected behavior"whwhwh "Test 3:"wh "  Problem with with . with function in module"wh "    ScriptBlock created at 'TopScript' scope"wh "    TopScript -calls-> ModFunction -dot-calls-> ScriptBlock:"whwh "  Expected behavior: ModFunction message after:  'Script block message'"wh "  Problem behavior:  TopScript message after:    'Script block message'"whwh "Results:"$global:message = "Global message"$message = "Top script message"wh "  Global message before:      '$global:message'"wh "  TopScript message before:   '$message'"simple_test_fun -script_block {$message = "Script block message"}wh "  TopScript message after:    '$message'"wh "  Global message after:       '$global:message'"whwh "Test 3 showed problem behavior"whwhwh "Test 4:"wh "  Confirm that problem scope is always scope where ScriptBlock is created"wh "    ScriptBlock created at 'f1' scope"wh "    TopScript -calls-> f1 -calls-> f2 -amp-calls-> ModFunction -dot-calls-> ScriptBlock:"whwh "  Expected behavior: ModFunction message after:  'Script block message'"wh "  Problem behavior:  f1 message after:           'Script block message'"whwh "Results:"$global:message = "Global message"$message = "Top script message"wh "  Global message before:      '$global:message'"wh "  TopScript message before:   '$message'"function f1 {    $message = "f1 message"    wh "  f1 message before:          '$message'"    f2 -script_block {$message = "Script block message"}    wh "  f1 message after:           '$message'"}function f2 {    param($script_block)    $message = "f2 message"    wh "  f2 message before:          '$message'"    simple_test_fun -script_block $script_block    wh "  f2 message after:           '$message'"}f1wh "  TopScript message after:    '$message'"wh "  Global message after:       '$global:message'"whwh "Test 4 showed problem behavior"whwhwh "Test 4:"wh "  Confirm that problem scope is always scope where ScriptBlock is created"wh "    ScriptBlock created at 'f1' scope"wh "    TopScript -calls-> f1 -calls-> f2 -amp-calls-> ModFunction -dot-calls-> ScriptBlock:"whwh "  Expected behavior: ModFunction message after:  'Script block message'"wh "  Problem behavior:  f1 message after:           'Script block message'"whwh "Results:"$global:message = "Global message"$message = "Top script message"wh "  Global message before:      '$global:message'"wh "  TopScript message before:   '$message'"function f1 {    $message = "f1 message"    wh "  f1 message before:          '$message'"    f2 -script_block {$message = "Script block message"}    wh "  f1 message after:           '$message'"}function f2 {    param($script_block)    $message = "f2 message"    wh "  f2 message before:          '$message'"    simple_test_fun -script_block $script_block    wh "  f2 message after:           '$message'"}f1wh "  TopScript message after:    '$message'"wh "  Global message after:       '$global:message'"whwh "Test 4 showed problem behavior"whwhwh "Test 5:"wh "  Problem with with . when module function invokes script (toplevel)"wh "    ScriptBlock created at 'TopScript' scope"wh "    TopScript -calls-> ModFunction -amp-calls-> Script -dot-calls-> ScriptBlock:"whwh "  Expected behavior: ModFunction message after:  'Script block message'"wh "  Problem behavior:  TopScript message after:    'Script block message'"whwh "Results:"$global:message = "Global message"$message = "Top script message"wh "  Global message before:      '$global:message'"wh "  TopScript message before:   '$message'"test_mod_to_script_toplevel -script_block {$message = "Script block message"}wh "  TopScript message after:    '$message'"wh "  Global message after:       '$global:message'"whwh "Test 5 showed problem behavior"whwhwh "Test 6:"wh "  Problem with with . when module function invokes script (function)"wh "    ScriptBlock created at 'TopScript' scope"wh "    TopScript -calls-> ModFunction -amp-calls-> Script -calls-> function -dot-calls-> ScriptBlock:"whwh "  Expected behavior: ModFunction message after:  'Script block message'"wh "  Problem behavior:  TopScript message after:    'Script block message'"whwh "Results:"$global:message = "Global message"$message = "Top script message"wh "  Global message before:      '$global:message'"wh "  TopScript message before:   '$message'"test_mod_to_script_function -script_block {$message = "Script block message"}wh "  TopScript message after:    '$message'"wh "  Global message after:       '$global:message'"whwh "Test 6 showed problem behavior"whwhwh "Test 7:"wh "  Problem with with & with function in module"wh "    ScriptBlock created at 'TopScript' scope"wh "    TopScript -calls-> ModFunction -amp-calls-> Script -calls-> function -dot-calls-> ScriptBlock:"whwh "  Expected behavior: ModFunction message after:  'Script block message'"wh "  Problem behavior:  TopScript message after:    'Script block message'"whwh "Results:"$global:message = "Global message"$message = "Top script message"wh "  Global message before:      '$global:message'"wh "  TopScript message before:   '$message'"ampersand_test_fun -script_block {set-variable -scope 1 -name "message" -value "Script block message"}wh "  TopScript message after:    '$message'"wh "  Global message after:       '$global:message'"whwh "Test 7 showed problem behavior"whwhwh "Test 8:"wh "  Problem with with invoke() method with function in module"wh "    ScriptBlock created at 'TopScript' scope"wh "    TopScript -calls-> ModFunction -amp-calls-> Script -calls-> function -dot-calls-> ScriptBlock:"whwh "  Expected behavior: ModFunction message after:  'Script block message'"wh "  Problem behavior:  TopScript message after:    'Script block message'"whwh "Results:"$global:message = "Global message"$message = "Top script message"wh "  Global message before:      '$global:message'"wh "  TopScript message before:   '$message'"method_test_fun -script_block {set-variable -scope 1 -name "message" -value "Script block message"}wh "  TopScript message after:    '$message'"wh "  Global message after:       '$global:message'"whwh "Test 8 showed problem behavior"


It appears that the $message in the scriptblock passed in is tied to the global scope e.g.:

function Test {     param(         [Parameter(Position=0,Mandatory=0)]         [scriptblock]$properties = {}     )     $defaults = {$message = "Hello, world!"}     Write-Host "Before running defaults, message is: $message"     . $defaults     #At this point, $message is correctly set to "Hellow, world!"     Write-Host "Aftering running defaults, message is: $message"     . $properties     #At this point, I would expect $message to be set to whatever is passed in,     #which in this case is "Hello from poperties!", but it isn't.       Write-Host "Aftering running properties, message is: $message"     # This works. Hmmm    Write-Host "Aftering running properties, message is: $global:message" } Export-ModuleMember -Function "Test" 

Outputs:

Before running defaults, message is: Aftering running defaults, message is: Hello, world!Executing properties, message is Aftering running properties, message is: Hello, world!Aftering running properties, message is: Hello from properties!

This would appear to be a bug. I'll prod the PowerShell MVP list to see if I can confirm this.