PowerShell 支持常量吗?
我想在 PowerShell 中声明一些整数常量。
有什么好的办法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想在 PowerShell 中声明一些整数常量。
有什么好的办法吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
使用
或
“常量”和“只读”之间的区别在于,可以通过删除只读变量(然后重新创建),
而不能删除常量变量(即使使用 -Force)。
有关详细信息,请参阅这篇 TechNet 文章。
Use
or
The difference between "Constant" and "ReadOnly" is that a read-only variable can be removed (and then re-created) via
whereas a constant variable can't be removed (even with -Force).
See this TechNet article for more details.
这是定义这样的常量的解决方案:
来自 http://poshcode.org/4063 的解决方案
Here is a solution for defining a constant like this:
Solution taken from http://poshcode.org/4063
要使用特定类型的值(例如 Int64),您可以显式转换 set-variable 中使用的值。
例如:
检查一下,
您会发现它是一个 Int64(而不是 Int32,对于值 100 来说这是正常的)。
To use a specific type of value, say Int64, you can explicitly cast the value used in set-variable.
For instance:
To check,
And you'll see that it is an Int64 (rather than Int32, which would be normal for the value 100).
我真的很喜欢 rob 的答案 提供的语法糖:
不幸的是,当您定义
模块中的>Set-Constant
函数。当从模块外部调用时,它将在定义Set-Constant
的模块作用域中创建一个常量,而不是调用者的作用域。这使得调用者看不到常量。以下修改后的函数修复了此问题。该解决方案基于问题此答案“powershell 模块有什么方法可以获取其调用者的范围吗? ”。
注释:
New-Module
行之所以存在,是因为该函数仅在从不同范围域调用时才起作用(又名会话状态)。您可以将该函数放入实际的模块文件 (.psm1) 中,但是您无法在同一模块中使用它!内存中模块使其可以从 PowerShell 脚本 (.ps1) 以及模块文件中按原样使用。-Mean
重命名为-Value
,以与Set-Variable
保持一致。Private
、ReadOnly
和AllScope
标志。只需将所需的值添加到PSVariable
构造函数,在上面的脚本中通过New-Object
调用。编辑 06/2023:
Set-Constant
和任何基于Set-Variable -Option Constant
的解决方案的缺点是, VSCode 的 PowerShell 扩展不支持导航到变量的定义,例如通过 Ctrl+Click 变量名称(请参阅此 GitHub 问题)。我当前的解决方法是像普通变量一样定义常量(因此 VSCode 在 AST 中看到它们的定义),然后通过使用 New-Variable -Force -Option Constant 重新定义它们来使它们成为常量代码>.与
Set-Variable
相反,New-Variable
命令可以覆盖现有变量。导出常量的典型模块现在看起来像这样:
这是一个完整的演示:
I really like the syntactic sugar that rob's answer provides:
Unfortunately his solution doesn't work as expected when you define the
Set-Constant
function in a module. When called from outside the module, it will create a constant in the module scope, whereSet-Constant
is defined, instead of the caller's scope. This makes the constant invisible to the caller.The following modified function fixes this problem. The solution is based on this answer to the question "Is there any way for a powershell module to get at its caller's scope?".
Notes:
New-Module
line is there because the function only works, when called from a different scope domain (aka session state). You could put the function into an actual module file (.psm1), but then you couldn't use it from within that same module! The in-memory module makes it usable as-is from both PowerShell scripts (.ps1) as well as module files.-Mean
to-Value
, for consistency withSet-Variable
.Private
,ReadOnly
andAllScope
flags. Simply add the desired values to the 3rd argument of thePSVariable
constructor, which is called in the above script throughNew-Object
.Edit 06/2023:
A disadvantage of both
Set-Constant
and any solution based onSet-Variable -Option Constant
is, that VSCode's PowerShell extension does not support navigation to the definition of the variable, e. g. by Ctrl+Click on the variable name (see this GitHub issue).My current workaround is to define the constants like normal variables (so VSCode sees their definition in the AST) and then make them constant by redefining them using
New-Variable -Force -Option Constant
. Contrary toSet-Variable
, theNew-Variable
command can overwrite existing variables.A typical module that exports constants, now looks like this:
Here is a full demo to play around with:
将
-option Constant
与Set-Variable
cmdlet 结合使用:现在
$myvar
的常量值为 100 并且无法修改。Use
-option Constant
with theSet-Variable
cmdlet:Now
$myvar
has a constant value of 100 and cannot be modified.通过
psvariable
。没有性能增益,并且可以通过反射随意更新:它不是一个真正的常量。如果您想要一个真正的 const,可以通过内联 C# 实现:
或者从 PowerShell 中最接近的方法是使用 PowerShell 类中的 static 属性(来自性能立场):
There is really 0 benefit from creating a constant thru a
psvariable
. There is no performance gain and it can be updated via reflection at will:It's not a real constant. If you want a real
const
the way is thru inline C#:Or the closest you can get to it from PowerShell is with a
static
property in a PowerShell Class (from a performance standpoint):PowerShell v5.0 应该允许
[static] [int] $variable = 42
[static] [DateTime] $thisday
等。
PowerShell v5.0 should allow
[static] [int] $variable = 42
[static] [DateTime] $thisday
and the like.