代码之家  ›  专栏  ›  技术社区  ›  Tom Hazel

PowerShell是否支持常量?

  •  93
  • Tom Hazel  · 技术社区  · 16 年前

    我想在PowerShell中声明一些整型常量。

    有什么好办法吗?

    6 回复  |  直到 7 年前
        1
  •  95
  •   Motti Strom    16 年前

    使用

    Set-Variable test -option Constant -value 100
    

    Set-Variable test -option ReadOnly -value 100
    

    “constant”和“read only”的区别在于,只读变量可以通过

    Remove-Variable test -Force
    

    但是常量变量不能被删除(即使使用-force)。

    this TechNet article 了解更多详细信息。

        2
  •  13
  •   rob Victor Ramos    13 年前

    下面是定义这样一个常量的解决方案:

    const myConst = 42
    

    解决方案取自 http://poshcode.org/4063

        function Set-Constant {
      <#
        .SYNOPSIS
            Creates constants.
        .DESCRIPTION
            This function can help you to create constants so easy as it possible.
            It works as keyword 'const' as such as in C#.
        .EXAMPLE
            PS C:\> Set-Constant a = 10
            PS C:\> $a += 13
    
            There is a integer constant declaration, so the second line return
            error.
        .EXAMPLE
            PS C:\> const str = "this is a constant string"
    
            You also can use word 'const' for constant declaration. There is a
            string constant named '$str' in this example.
        .LINK
            Set-Variable
            About_Functions_Advanced_Parameters
      #>
      [CmdletBinding()]
      param(
        [Parameter(Mandatory=$true, Position=0)]
        [string][ValidateNotNullOrEmpty()]$Name,
    
        [Parameter(Mandatory=$true, Position=1)]
        [char][ValidateSet("=")]$Link,
    
        [Parameter(Mandatory=$true, Position=2)]
        [object][ValidateNotNullOrEmpty()]$Mean,
    
        [Parameter(Mandatory=$false)]
        [string]$Surround = "script"
      )
    
      Set-Variable -n $name -val $mean -opt Constant -s $surround
    }
    
    Set-Alias const Set-Constant
    
        3
  •  9
  •   Paolo Tedesco    16 年前

    使用 -option Constant Set-Variable Cmdlet:

    Set-Variable myvar -option Constant -value 100
    

    现在 $myvar 常量值为100,无法修改。

        4
  •  9
  •   Mike Shepard    13 年前

    要使用特定类型的值(比如int64),可以显式转换set变量中使用的值。

    例如:

    set-variable -name test -value ([int64]100) -option Constant
    

    检查一下,

    $test | gm
    

    您将看到它是一个int64(而不是int32,对于值100来说这是正常的)。

        5
  •  1
  •   zett42    7 年前

    我真的很喜欢那种 rob's answer 提供:

    const myConst = 42
    

    不幸的是,当你 Set-Constant 变成一个 模块 . 当从模块外部调用时,它将在模块范围内创建一个常量,其中 设置常量 是定义的,而不是 呼叫者范围 . 这使调用方看不到常量。

    下面的修改函数修复了这个问题。解决方案基于 this answer 关于这个问题 "Is there any way for a powershell module to get at its caller's scope?" .

    function Set-Constant {
        <#
        .SYNOPSIS
            Creates constants.
        .DESCRIPTION
            This function can help you to create constants so easy as it possible.
            It works as keyword 'const' as such as in C#.
        .EXAMPLE
            PS C:\> Set-Constant a = 10
            PS C:\> $a += 13
    
            There is a integer constant declaration, so the second line return
            error.
        .EXAMPLE
            PS C:\> const str = "this is a constant string"
    
            You also can use word 'const' for constant declaration. There is a
            string constant named '$str' in this example.
        .LINK
            Set-Variable
            About_Functions_Advanced_Parameters
        #>
        [CmdletBinding()]
        param(
            [Parameter(Mandatory=$true, Position=0)] [string] [ValidateNotNullOrEmpty()] $Name,
            [Parameter(Mandatory=$true, Position=1)] [char] [ValidateSet("=")] $Link,
            [Parameter(Mandatory=$true, Position=2)] [object] [ValidateNotNullOrEmpty()] $Value
        )
    
        $var = New-Object System.Management.Automation.PSVariable -ArgumentList @(
            $Name, $Value, [System.Management.Automation.ScopedItemOptions]::Constant
        )
    
        $PSCmdlet.SessionState.PSVariable.Set( $var )
    }
    
    Set-Alias const Set-Constant
    

    笔记:

    • 功能 只有 工作,从调用时 外部 定义模块的位置。这是预期的用例,但是我想添加一个检查,它是否是从同一个模块调用的(在这种情况下 Set-Variable -scope 1 如果我知道怎么做的话。
    • 我已重命名参数 -Mean -Value ,以符合 Set-Variable .
    • 该函数可以扩展为可选地设置 Private , ReadOnly AllScope 旗帜。只需将所需的值添加到 PSVariable constructor ,在上面的脚本中通过调用 New-Object
        6
  •  -3
  •   wonea Ilya Smagin    8 年前

    PowerShell v5.0应允许

    [静态][整数]$变量=42

    [静态][日期时间]$今天

    等等。