Powershell guru, please specify the scope variable

I read that the variables inside the scope are available in the current scope when the script function is a search point.

It's true? This is very strange and unusual, I think ...

May I explain the reasons for this. Example: my-f sets $my-f-var to some integer, say 2:

 PS1> . .\my-f-script.ps1 PS1> my-f PS1> $my-f-var 2 

I would expect that $my-f-var not available, because inside the function! Is there a way to make variables private or a way to call a function without a script lookup point?

+6
scope powershell
source share
1 answer

Variables inside a function are local to this scope unless you dot the call to the function name. When you specify the source of the script, the top-level variables of the script are effectively imported into the current scope along with function definitions, for example:

 PS> '$scriptvar = 2; function my-f { ${my-f-var} = 2 }' > my-f-script.ps1 PS> Remove-Variable scriptvar, my-f-var Remove-Variable : Cannot find a variable with name 'scriptvar'. Remove-Variable : Cannot find a variable with name 'my-f-var'. PS> . .\my-f-script.ps1 PS> $scriptvar 2 PS> ${my-f-var} PS> 

Note that $ {my-f-var} is not defined in the local scope. However, if I 'dot' calls a function, then its contents are launched in the current area, for example:

 PS> . my-f PS> ${my-f-var} 2 

In this case, the variable set in the function is set in the current (calling) area due to the "point" used to call it.

A few points: you can access variables in different areas using Get-Variable -scope or more convenient (if less flexible) global, script, local and private modifiers. When you access a variable inside a function where the variable is defined in a higher scope, you can read it just fine. But when you install it, PowerShell essentially does a β€œcopy to write” of this variable - creating a new copy with this function down (that is, to the other functions that it calls). If you really want to change a variable with a higher scope, you can use $ global: Foo or $ script: Foo to change in these areas.

The local scope is useful if you want to accidentally avoid using a variable defined outside of your function. Another MVP picked it up at the last MVP summit, and it seems like one of those "best practice" tips. Here's the script:

 PS> $foo = 'bad' PS> function testscope { $fooo = 'good'; "The value of `$fooo is $foo" } PS> testscope The value of $fooo is bad 

Note that in this case, using $foo inside a function is a typo, but coincidentally there is such a name typo'd. This was not the purpose of this function, and it does not work correctly, although in this case it is difficult to understand. Below we can use the local qualifier to make sure that the function only looks at the local scope for the variable. He does not find it because of a typo, but at least the error is a little easier to notice.

 PS> function testscope { $fooo = 'good'; "The value of `$fooo is $local:foo" } PS> testscope The value of $fooo is 

The private scope is useful when you do not want certain variables to be visible to other functions that your function calls.

+14
source share

All Articles