[英]Does PowerShell support constants?
我想在 PowerShell 中聲明一些整數常量。
有什么好的方法可以做到這一點嗎?
用
Set-Variable test -Option Constant -Value 100
或者
Set-Variable test -Option ReadOnly -Value 100
“常量”和“只讀”之間的區別在於,可以通過以下方式刪除(然后重新創建)只讀變量
Remove-Variable test -Force
而不能刪除常量變量(即使使用 -Force)。
有關更多詳細信息,請參閱此 TechNet 文章。
這是定義這樣的常量的解決方案:
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
要使用特定類型的值,例如 Int64,您可以顯式轉換 set-variable 中使用的值。
例如:
set-variable -name test -value ([int64]100) -option Constant
去檢查,
$test | gm
您會看到它是一個 Int64(而不是 Int32,這對於值 100 來說是正常的)。
將-option Constant
與Set-Variable
cmdlet 一起使用:
Set-Variable myvar -option Constant -value 100
現在$myvar
的常量值為 100 且無法修改。
我真的很喜歡rob 的答案提供的語法糖:
const myConst = 42
不幸的是,當您在模塊中定義Set-Constant
函數時,他的解決方案無法按預期工作。 當從模塊外部調用時,它將在模塊作用域中創建一個常量,其中定義了Set-Constant
,而不是調用者的作用域。 這使得常量對調用者不可見。
以下修改后的函數修復了這個問題。 該解決方案基於對“powershell 模塊是否有辦法進入其調用者的范圍?”這個問題的回答。 .
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] $Name,
[Parameter(Mandatory=$true, Position=1)] [char] [ValidateSet("=")] $Link,
[Parameter(Mandatory=$true, Position=2)] [object] $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
構造函數的第三個參數中,在上面的腳本中通過New-Object
調用它。PowerShell v5.0 應該允許
[靜態] [int] $variable = 42
[靜態] [日期時間] $thisday
之類的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.