PowerShell支持常量吗?

时间:2010-04-09 14:22:01

标签: powershell constants

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

有没有好办法呢?

6 个答案:

答案 0 :(得分:98)

使用

Set-Variable test -option Constant -value 100

Set-Variable test -option ReadOnly -value 100

“常量”和“只读”之间的区别在于可以通过

删除(然后重新创建)只读变量
Remove-Variable test -Force

而无法删除常量变量(即使使用-Force)。

有关详细信息,请参阅this TechNet article

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

答案 2 :(得分:9)

-option ConstantSet-Variable cmdlet一起使用:

Set-Variable myvar -option Constant -value 100

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

答案 3 :(得分:9)

要使用特定类型的值,例如Int64,您可以显式地转换set-variable中使用的值。

例如:

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

要检查,

$test | gm

你会发现它是一个Int64(而不是Int32,这对于值100来说是正常的。)

答案 4 :(得分:1)

我真的很喜欢rob's answer提供的语法糖:

const myConst = 42

不幸的是,当您将Set-Constant放入模块时,他的解决方案无法按预期工作。从模块外部调用时,它将在定义了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
  • 该功能可以扩展为可选地设置PrivateReadOnlyAllScope标志。只需将所需的值添加到PSVariable constructor的第3个参数中即可,在上面的脚本中通过New-Object调用了该参数。

答案 5 :(得分:-3)

PowerShell v5.0应该允许

[静态] [int] $ variable = 42

[静态] [日期时间] $ thisday

之类的。

相关问题