What is the setlocal / endlocal equivalent for PowerShell?

后端 未结 4 2249
时光说笑
时光说笑 2021-02-15 15:24

Objective

Isolate environmental variable changes to a code block.

Background

If I want to create a batch scrip

4条回答
  •  隐瞒了意图╮
    2021-02-15 15:54

    Forgive me if I've missed something as there parts of this post I'm a bit unclear on.

    I would use the scope modifies $local, $script and $global modifiers.

    Example

    $env:TEST="EN"
    function tt {
      $local:env:TEST="GB"
      ($local:env:TEST)
    }
    
    $t = {
      $local:env:TEST="DE"
      ($local:env:TEST)
    }
    
    ($env:TEST)
    tt
    ($env:TEST)
    . $t
    ($env:TEST)
    

    Output with comments

    EN     # ($env:TEST)
    GB     # tt
    EN     # ($env:TEST)
    DE     # . $t
    EN     # ($env:TEST)
    

提交回复
热议问题