当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 常量和只读变量

作者:精品下载站 日期:2024-12-14 17:18:39 浏览:17 分类:玩电脑

PowerShell 常量和只读变量


PowerShell 有一个内置的 cmdlet New-Variable 用于创建只读变量和常量变量。该命令使用参数-Option Constant来创建常量变量。

Powershell中只读变量和常量变量的区别在于,只读变量只能被赋值一次,并且以后不能修改它们的值。常量变量与只读变量类似,但无法删除或更改它们,即使使用Remove-Variable cmdlet 也是如此。

在本文中,我们将讨论如何在 PowerShell 中创建只读变量和常量变量。

在 PowerShell 中创建只读变量

在 PowerShell 中使用 Set-Variable cmdlet 和参数 -Option Readonly 创建只读变量。

# Create read-only variable in PowerShell using Set-Variable
Set-Variable -Name piValue -Value "3.14" -Option ReadOnly

# Print the variable value
$piValue

在上面的 PowerShell 脚本中,Set-Variable cmdlet 使用 -Name 参数指定变量 `piValue` 和 `- Value` 参数将值分配给变量,最后将 `-Option` 参数设置为ReadOnly

上述脚本的输出创建一个只读变量,如下所示:

PS D:\> Set-Variable -Name piValue -Value "3.14" -Option ReadOnly 
                                                    
PS D:\> $piValue                                                                                                        
3.14

如果尝试将只读变量值更改或修改为另一个值,则会抛出异常。

$piValue = 3.145  

通过为只读变量赋值会引发异常,如下所示

[玩转系统] PowerShell 常量和只读变量

在 PowerShell 中创建常量变量

要创建常量变量,请使用 Set-Variable cmdlet,并将 `-Option` 参数设置为 `Constant`。

# Create PowerShell constant variable
Set-Variable -Name dbServerIPAddress -Value "10.1.1.1" -Option Constant

# Print the PowerShell constant variable value
$dbServerIPAddress

在上面的 PowerShell 脚本中,Set-Variable cmdlet 使用 Name 参数指定变量 `dbServerIPAddress` 和 `-Value ` 参数将常量值分配给变量,最后 `-Option` 参数设置为常量

上述脚本的输出创建 PowerShell 常量变量 $dbServerIPAddress 并为其分配值。

PS D:\> Set-Variable -Name dbServerIPAddress -Value "10.1.1.1" -Option Constant                                         PS D:\>                                                                                                                 

PS D:\> $dbServerIPAddress                                                                                              10.1.1.1

PS D:\>     

如果尝试修改PowerShell常量变量值,将会抛出异常。

$dbServerIPAddress = 10.1.1.2

例外情况如下:

PS D:\> Set-Variable -Name dbServerIPAddress -Value "10.1.1.1" -Option Constant                                         PS D:\>                                                                                                                 
PS D:\> $dbServerIPAddress                                                                                              10.1.1.1

PS D:\> $dbServerIPAddress = 10.1.1.2                                                                                   Cannot overwrite variable dbServerIPAddress because it is read-only or constant.
At line:1 char:1
+ $dbServerIPAddress = 10.1.1.2
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (dbServerIPAddress:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable

PS D:\>         

比较只读变量和常量变量

在PowerShell中,只读变量和常量变量都不能修改其值,它们之间的主要区别在于变量的删除。

可以使用Remove-Variable cmdlet 删除只读变量,但无法删除常量变量。

# Remove read-only variable
Remove-Variable -Name "piValue" -Force

# Remove PowerShell constant variable
Remove-Variable -Name "dbServerIPAddress" -Force

在上面的 PowerShell 脚本中,Remove-Variable cmdlet 使用 -Name 参数指定只读变量名称,并使用 -Force 参数来指定只读变量。强制删除参数。它删除只读变量。

在尝试删除 PowerShell 常量变量时,它会引发异常,如下所示:

PS D:\> Remove-Variable -Name "piValue" -Force
                                                                          
PS D:\> Remove-Variable -Name "dbServerIPAddress" -Force                                                                Remove-Variable : Cannot remove variable dbServerIPAddress because it is constant or read-only. If the variable is
read-only, try the operation again specifying the Force option.
At line:1 char:1
+ Remove-Variable -Name "dbServerIPAddress" -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (dbServerIPAddress:String) [Remove-Variable], SessionStateUnauthorizedAccess
   Exception
    + FullyQualifiedErrorId : VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand

PS D:\>  

酷提示:如何在 PowerShell 中清除变量值!

结论

我希望上述有关如何使用 Set-Variable cmdlet 创建 PowerShell 常量和只读变量的文章对您有所帮助。

在 PowerShell 中创建只读变量或常量变量时,请使用 Set-Variable cmdlet,并将 -Option 参数值设置为“ReadOnly”或“Constant ”分别为“”。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯