Windows PowerShell Home
Functions in Script Modules do not inherit variables from the scope of their caller
3/5/2014 10:57:09 AM
User(s) can reproduce this bug
When you call a function that's been imported from a psm1 Script Module, variables such as $ErrorActionPreference that have been set in the caller's local scope are not seen by the module's function, unless the calling code sets its variables in the Global scope.
How often does this happen?
Have you seen this problem before in this product?
Yes, this happens in previous released versions
Create a script module named TestModule.psm1 with the following code:
param ( )
Write-Host "Effective ErrorActionPreference: $ErrorActionPreference"
Write-Error "This is an error from Test-Function."
Create a ps1 file in the same directory, with the following code:
$ErrorActionPreference = 'SilentlyContinue'
Then execute the ps1 script from the PowerShell console. (Note: Do not run the test script using "F5" in the ISE, as this executes the script's code in the Global scope and invalidates the test.) You will see that even though the caller set $ErrorActionPreference to SilentlyContinue (in its "script:" scope), the module function still has an effective setting of 'Continue' and the error is output to the console.
The functions exported from script modules should automatically inherit variables from their caller. (At a minimum, the PowerShell preference variables should behave this way, if not all variables in the caller's scopes.)
to post a comment.
Please enter a comment.
to post a workaround.
Please enter a workaround.
© 2014 Microsoft