Closed ArtisanByteCrafter closed 3 years ago
Thank you @ArtisanByteCrafter, you are spot on! The issue is scope related. When a module function get to the part where it tries to access the Filter variables there are just no way (that I have found) to access any local variables from a function, just like you demonstrate.
You use global: in your example, does that mean that script scope failed? I, too, dislike using Global variables, so it would have been nice to figure out a different way of doing it. But global variables does work, just like you demonstrate.
Thank you @ArtisanByteCrafter, you are spot on! The issue is scope related. When a module function get to the part where it tries to access the Filter variables there are just no way (that I have found) to access any local variables from a function, just like you demonstrate.
You use global: in your example, does that mean that script scope failed? I, too, dislike using Global variables, so it would have been nice to figure out a different way of doing it. But global variables does work, just like you demonstrate.
When a module function get to the part where it tries to access the Filter variables there are just no way (that I have found) to access any local variables from a function, just like you demonstrate.
$PSCmdlet.SessionState
will contain the session state of the caller. So for your Update-Filter
command you would need to do something like this:
function Test-PublicFacingCommand {
[CmdletBinding()]
param([string] $VariableName)
end {
$value = GetCallerVariableValue -Context $PSCmdlet -VariableName $VariableName
}
}
function GetCallerVariableValue {
[CmdletBinding()]
param(
[System.Management.Automation.PSCmdlet] $Context,
[string] $VariableName
)
end {
return $Context.SessionState.PSVariable.GetValue($VariableName)
}
}
I'm using version 1.6.6 for this issue.
If I specify this, it works perfect:
However, by simply encapsulating the same code into a function and attempting to pass the values in as parameters, I receive an error:
Error received:
I believe this might be scope related, as I can get around it this way:
I'd really like to avoid global variables if at all possible, but i wanted to bring it up as how i managed to work around it.
Thanks again for all your hard work on this module!