r/PowerShell • • 4h 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.

8 Upvotes

23 comments sorted by

View all comments

Show parent comment

1

u/surfingoldelephant 2h ago edited 1h ago

If I understand OP correctly, they want to lookup a variable in an arbitrary ancestral scope while avoiding locals of the same name.

Say $foo is initialized in test1.ps1 which calls test2.ps1. In test2.ps1, $foo is visible normally, but not if you specify -Scope Script, since that limits the lookup to just test2.ps1's scope.

It seems like looping over an incremented scope number is what they're looking for. Something like this: Get-AncestralVariable

u/SymfonyWonderer2, just note that this won't work across module boundaries.

1

u/jborean93 1h ago

Thanks for the clarification, re-reading it and I'm still somewhat confused as to what the actual purpose/request is but I'll leave that for OP to clarify.

1

u/BlackV 1h ago

Ya, You'd need to see the history of their previous posts a bunch of context comes from there

1

u/surfingoldelephant 1h ago

Yeah, it's not particularly clear. OP has another post which they didn't link to.

It seems it's for debugging purposes/inspecting variables in different scopes. Or maybe it's just a learning exercise, I'm not entirely sure. Anyway, I think looping over scope numbers solves their immediate issue. OP can clarify like you said.

1

u/SymfonyWonderer2 19m ago

The answer is Yes, all three.

1

u/SymfonyWonderer2 22m ago

Thanks! That seem like what I want, except I will first check that the value from GV are right, and if not, search using scope. One thing though, I understood that modules shared the scope of the script they were loaded into. You seem to be saying that making my function a module will break it.