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

时间:2017-11-04 23:37:00

标签: powershell constants

## 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允许更改值。

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

背景:

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

2 个答案:

答案 0 :(得分: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

Name                           Value
----                           -----
c                              x
Get-Variable : Cannot find a variable with the name 'c'.
At line:3 char: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

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

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
Cannot overwrite variable c because it is read-only or constant.
At line:3 char: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
Cannot overwrite variable c because it is read-only or constant.
At line:3 char:5
+     $c = "xxxx"
+     ~~~~~~~~~~~
    + CategoryInfo          : WriteError: (c:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable

x

答案 1 :(得分: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
#________________________________________________

注意:全局和脚本的差异是一个范围问题。有关详细信息,请参阅here