`Get-Variable -Scope <number>` should (possibly) return the (inherited) read scope
See original GitHub issueSummary of the new feature / enhancement
As a user I would expect the Get-Variable -Scope <number>
to return the (inherited) read scope of the concerned variable.
(or at least have an easy way to do so with e.g. an extra parameter.)
function Child {
Write-Host 'Parent DebugPreference:' (Get-Variable 'DebugPreference' -Scope 1 -ValueOnly)
}
function Parent {
Write-Host 'Parent DebugPreference:' $DebugPreference
Child
}
Parent
Expected:
Parent DebugPreference: SilentlyContinue
Parent DebugPreference: SilentlyContinue
Actual:
Parent DebugPreference: SilentlyContinue
Get-Variable: C:\Users\Gebruiker\Scripts\Test-Scope\Test-Scope.ps1:10
Line |
10 | … ebugPreference:' (Get-Variable 'DebugPreference' -Scope 1 -ValueOnly)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot find a variable with the name 'DebugPreference'.
Proposed technical implementation details (workaround)
function Child {
for ($i = 1; $i -lt (Get-PSCallStack).Count; $i++) {
$ParentDebugPreference = Get-Variable 'DebugPreference' -Scope $i -ValueOnly -ErrorAction SilentlyContinue
if ($Null -ne $ParentDebugPreference) { break }
}
Write-Host 'Parent DebugPreference:' $ParentDebugPreference
}
function Parent {
Write-Host 'Parent DebugPreference:' $DebugPreference
Child
}
Parent
Issue Analytics
- State:
- Created a year ago
- Comments:15
Top Results From Across the Web
about Scopes - PowerShell
A child scope doesn't inherit the variables, aliases, and functions from the parent scope. Unless an item is private, the child scope can...
Read more >Building Better Scripts with PowerShell Scopes for Variables
A way to use scopes to reduce conflicts is to use the private scope. Using the private scope disables inheritance on that specific...
Read more >Getting All Variables In Scope - javascript
@Jason - No, the question is clear. Inside a function the variables in scope will include global variables, this , arguments , parameters...
Read more >Using flow variables | Apigee Edge
As you'll see, variables have scope, and where they are accessible depends in part on when they are created in the API proxy...
Read more >Powershell Global Variable: Explained With Examples
In PowerShell, the variable scope is the area where a variable is ... To retrieve the value of a global variable, you can...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I am trying to figure out what the actual requirement is and if you need to use
get-variable
at all. If you want the inherited $x won’t$x
provide what you want in almost all cases ? Some example code to show why I thinkGet-Variable
isn’t needed.This shows that in B and C, we can test for “X was (not) set locally”, “X has a value” therefore it’s simple to get to “X was inherited”, without caring where from If X is a parameter or a preference variable set via a parameter, I’m trying to think of a real-world use case for wanting to know what it was before it was set here. “$x from all higher scopes but not this one” is something I have never needed myself, so I’m having trouble imagining a use for it.
In a more complicated example.
This shows an example where we don’t want the present behaviour of
Get-Variable
to change (we want to know if a variable was or was not set in scope 1) but maybe a “AND ABOVE” switch could be helpful - but as a I said my imagination isn’t giving me any cases.As @SeeminglyScience says variables don’t inherit out of modules and I have a post about that, and how preference variables can be passed here:
https://devblogs.microsoft.com/powershell-community/how-to-have-more-control-of-preferences-in-functions-and-the-role-of-modules-on-inheritance/ it’s not clear from the question if you are trying to solve a problem with
$DebugPreference
but that post might help if you are.The way I’d recommend implementing this is to place your function in a different module. e.g.