如何在PowerShell中创建全局常量变量

5
## 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允许更改值。
是否有一种方法创建真正的全局常量变量?
背景:
我有一个主脚本。主脚本加载了几个模块。在所有模块和主脚本中,我需要一些固定的文件和注册表路径。因此,我希望将这些路径声明为全局常量。

4
$global:c的值从不改变 - 当您在test内部执行 $c = "xxxx"时,PowerShell会创建一个局部副本$c - 在调用test之后添加Write-Host $c,您会看到这个局部副本的值。 - Mathias R. Jessen
4
选项常量,所有范围 - user4003407
2个回答

9
全局变量 $c 保持不变,但是通过赋值 $c = "xxxx" 定义了另一个本地变量 $c,它采用新的值并在本地环境中屏蔽全局变量。
演示:
PS C:\> Set-Variable -Name c -Value "x" -Option Constant -Scope Global -Force
PS C:\> function test {
>>     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:\> test

名称                          值
----                           -----
c                              x
Get-Variable : 找不到名称为“c”的变量。
位于第3行字符5
+     Get-Variable -Name c -Scope Local
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (c:String) [Get-Variable],项NotFoundException
    + 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:\> function test {
>>     $global:c
>>     $global:c = "xxxx"
>>     $global:c
>> }
>>
PS C:\> test
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:\> function test {
>>     $c
>>     $c = "xxxx"
>>     $c
>> }
>>
PS C:\> test
x
由于变量是只读或常量,因此无法覆盖变量 c。
位于第3行的字符:5
+     $c = "xxxx"
+     ~~~~~~~~~~~
    + CategoryInfo          : WriteError: (c:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable

x

1

完整答案:

#________________________________________________
# 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
#________________________________________________

注意:全局和脚本的区别是作用域问题。有关详细信息,请参见这里

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接