On Aug 15, 9:36*pm, "Kiron" <Ki...@xxxxxx> wrote:
Quote:
> Hi tojo,
> In tmp.ps1 -- when the param -ExportVariable is not null -- Set-Variable sets variables in the Parent (1) scope, i.e. tmp2.ps1's scope when tmp.ps1 is called from tmp2.ps1, because the If statement does not create a child scope, so Set-Variable is not resetting the Local (0) scope variables. If instead of setting the new/exported variable in an 'Then' block you use a function/scriptblock to set it, the scope of the exported variable's scope would have to be the Grandparent's (2) because a Function/scriptblock does create a new scope.
>
> function ev ([string[]]$vns) {
> *foreach ($vn in $vns) {
> * sv $vn ((gv $vn -s 1).value * 3) -s 2
> *}
>
> }
>
> & {
> *& {
> * $v1 = 1
> * *if ($v1 -eq 1) {sv v1 2 -s 1}
> * *write-host `$v1 is $v1 in the Local scope
> * }
> *write-host `$v1 is $v1 in the Parent scope
>
> }
>
> # no $v1 in the Global scope
> $v1
>
> & {
> *& {
> * $v1 = 1
> * *if ($v1 -eq 1) {ev v1}
> * *write-host `$v1 is $v1 in the Local scope
> * }
> *write-host `$v1 is $v1 in the Parent scope
>
> }
>
> # no $v1 in the Global scope
> $v1
>
> Jeffrey Snover explains it here:http://blogs.msdn.com/powershell/arc...trolling-the-s...
>
> --
> Kiron Sweet. Thanks. The control over scope is unprecedented in PowerShell
as far as I can tell, but the potential for confusion is unprecedented
as well.