r/PowerShell • u/SymfonyWonderer2 • 5d ago
Question How to exclude showing Get-Variable output for -Scope 0
I want to call myfunc function that runs the statement:
$value = ( Get-Variable -Name $var_name -Valueonly -Scope Script )
That will show the GM's output for the instance of the variable named in the myfunc's$var_name parameter, a string, that was in scope when the myfunc was called, no mater where or what scope the call was made in, but myfunc must ignore a local variable with the same name in favor of the same named variable outside of the myfunc . If there are multiple variables with the same name outside of myfunc, then only the one that is in scope, that is not eclipsed, is shown (as I think already happens).
myfunc is currently defined at the script file's script scope, so it be callable from anywhere in the script where it's defined or 'included', but if the calling statement is nested deeply in a chain of functions or 'included' scripts, will the$var_name parameter need to be 'typed' as [ref] to make this work?
Google suggests the following to eliminate myfunc variables, their version lists:
-Scope 0,1,2,Global,Script
But, I don't want to keep changing this list when I call myfunc from deeper scopes, so I changed the scope option to:
-Scope 0,1,2,3,4,5,6,7,8,9,Global,Script
I don't expect to need that, but without a All, Allscope, or notLocal option setting, or a way to dynamically determine the scope of the variable from its name or the name from its value. I pretty much need to include all reasonable scopes that I might one day need.
Here is the rest of that suggested line of code:
$value = (
Get-Variable `
-Name $var_name `
-Valueonly `
-Scope '0,1,2,3,4,5,6,7,8,9,Global,Script' `
-ErrorAction SilentlyContinue
|
Where-Object { $_.Scope -ne 0 }
)
I'll check the last error auto-variable and output my own error message if there is an error.
When is a string not a string? I don't know, but PowerShell gives me this error when I try running the suggested line of code, above, after fixing the missing quotes that Copilot left off:
Line |
200 | Get-Variable `
| ~~~~~~~~~~~~~~
| Cannot process argument because the value of parameter "Scope" is not valid. Valid values are "Global", "Local", or "Script", or a number relative to the current scope (0 through the number of scopes where 0 is the current scope and 1 is its parent). Change the value of the "Scope" parameter and run the operation again.
In another thread, someone said AI code was 'slop', here is an example!
Thank you.
5
u/dodexahedron 5d ago
If you specify an explicit scope with get-variable, you will be given the value of that variable from that scope or else nothing at all. It does not perform the normal walk through the currently visible scopes when you give it that parameter.
What is your question though, and what is the problem you are trying to solve?
I think you're overthinking something that you shouldn't be trying to assert control over.
1
u/jborean93 4d ago
Just use
$script:varNameor if the variable really must be dynamic thenGet-Variable -Name $varName -Scope Script -ValueOnlyThen when running the script
Even better would be to not have a conflict with the name or structure your functions so they don't rely on things set output the scope and have it be passed in.
1
u/BlackV 4d ago
Splatting ;)
1
u/coffeewap-vpc 3d ago
What's the reason for using scopes in this way? Can you show us what your myfunc function does? is the value that myfunc is returning something that is computed within myfunc?
Can you explain a bit more what you are trying to solve here? Feels a bit ambigous, if you could explain a bit more detailed maybe we could be of more help
1
u/KevMar Community Blogger 4d ago
Use scope 0 if you want to include the local scope (and above). Use scope 1 if you want to exclude the local scope but want whatever is in the parent scope and above.
You don't need to check every scope. If the variable is set in scope 5, it exists in 4,3,2,1,0 unless another variable is redefined in one of those other scopes.
Another trick you can do is use local variables with unique names that are unlikely to be reused in other scopes. I have used the variable name
${*map}before for this exact reason.2
u/purplemonkeymad 4d ago
Lots of people giving you suggestions, but the reason for the error at the end is this:
You have quotes around the value. This means you are not giving multiple values, but actually passing the literal string
0,1,2,3,4,5,6,7,8,9,Global,Script! Remove the quotes if you are wanting to pass those as separate multiple values.