【问题标题】:How to create global constant variables in PowerShell如何在 PowerShell 中创建全局常量变量
【发布时间】:2017-11-04 23:36:59
【问题描述】:
## My try to create a global constant 
Set-Variable -Name c -Value "x" -Option Constant -Scope Global -Force

Write-Host $c  ## -> x
$c = "y"       ## -> WriteError: (C:String) [], SessionStateUnauthorizedAccessException 
               ## -> VariableNotWritable
Write-Host $c  ## -> x

function test {
    Write-Host $c  ## -> x
    $c = "xxxx"
    Write-Host $c  ## -> xxxx
}

test 

我的变量$c 是全局可访问的,但并非在所有情况下都保持不变。尝试更改函数test() 中的值,PowerShell 允许更改值。

有没有办法创建一个真正的全局常量变量?

背景:

我有一个主脚本。主脚本加载了一些模块。通过所有模块和主脚本,我需要一些固定的文件和注册表路径。 所以我想将这些路径声明为全局常量。

【问题讨论】:

  • $global:c 的值永远不会改变 - 当你在 test 中执行 $c = "xxxx" 时,powershell 会创建 $c 的本地副本 - 在调用 Write-Host $c 之后添加 Write-Host $c 和你会看到
  • -Option Constant, AllScope

标签: powershell constants


【解决方案1】:

global 变量 $c 保持不变,但通过赋值 $c = "xxxx" 定义了另一个 local 变量 $c,它采用新值并屏蔽全局本地上下文中的变量。

演示:

PS C:\> Set-Variable -Name c -Value "x" -Option Constant -Scope Global -Force PS C:\> 功能测试{ >> Get-Variable -Name c -Scope Global >> Get-Variable -Name c -Scope Local >> $c = "xxxx" >> Get-Variable -Name c -Scope Global >> Get-Variable -Name c -Scope Local >> } >> PS C:\> 测试 名称 值 ---- ----- c x Get-Variable:找不到名为“c”的变量。 在行:3 字符:5 + Get-Variable -Name c -Scope Local + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (c:String) [Get-Variable], ItemNotFoundException + FullyQualifiedErrorId:VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand c x c xxxx

第一个Get-Variable -Name c -Scope Local调用失败,因为还没有定义局部变量$c

可以通过在变量/常量前加上正确的范围来避免这个问题:

PS C:\> Set-Variable -Name c -Value "x" -Option Constant -Scope Global -Force PS C:\> 功能测试{ >> $全局:c >> $全局:c = "xxxx" >> $全局:c >> } >> PS C:\> 测试 X 无法覆盖变量 c,因为它是只读的或常量。 在行:3 字符:5 + $global:c = "xxxx" +~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (c:String) [], SessionStateUnauthorizedAccessException + FullyQualifiedErrorId : VariableNotWritable X

或通过为所有范围定义常量:

PS C:\> Set-Variable -Name c -Value "x" -Option Constant, AllScope -Force PS C:\> 功能测试{ >> $c >> $c = "xxxx" >> $c >> } >> PS C:\> 测试 X 无法覆盖变量 c,因为它是只读的或常量。 在行:3 字符:5 + $c = "xxxx" + ~~~~~~~~~~~ + CategoryInfo : WriteError: (c:String) [], SessionStateUnauthorizedAccessException + FullyQualifiedErrorId : VariableNotWritable X

【讨论】:

    【解决方案2】:

    完整答案:

    #________________________________________________
    # Example1, without prefix
    #________________________________________________
    $c="val1"
    
    # Print output c variable
    $c
    
    function test
    {
        $c="val2"
    }
    
    test
    
    # Print output c variable (c not change)
    $c
    
    #________________________________________________
    
    # Example2, with prefix global
    #________________________________________________
    $global:c="val1"
    
    # Print output c variable
    $global:c
    
    function test2
    {
        $global:c="val2"
    }
    
    test2
    
    # Print output c variable (c change)
    $global:c
    
    #________________________________________________
    
    # Example3, with prefix script
    #________________________________________________
    $script:c="val1"
    
    # Print output c variable
    $script:c
    
    function test3
    {
        $script:c="val2"
    }
    
    test3
    
    # Print output c variable (c change)
    $script:c
    
    #________________________________________________
    
    # Example 4, with get and set variable --> see answer of Ansgar Wiechers
    #________________________________________________
    

    注意:全局和脚本的差异是一个范围问题。详情请见here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-29
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多