PowerShell 中带有括号的环境变量名称,例如 %ProgramFiles(x86)%?
在PowerShell脚本中,如何获取名称中包含括号的环境变量的值?
使事情变得复杂的是,某些变量的名称包含括号,而其他变量的名称相似但不带括号。例如(使用cmd.exe
):
C:\>set | find "ProgramFiles"
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
我们看到%ProgramFiles%
与%ProgramFiles(x86)%
不同。
我的 PowerShell 代码以一种奇怪的方式失败,因为它忽略了括号后面的环境变量名称部分。由于这恰好与不同的名称匹配,但现有的环境变量我不会失败,我只是得到了错误变量的正确值。
这是 PowerShell 脚本语言中的一个测试函数来说明我的问题:
function Do-Test
{
$ok = "C:\Program Files (x86)" # note space between 's' and '('
$bad = "$Env:ProgramFiles" + "(x86)" # uses %ProgramFiles%
$d = "${ Env:ProgramFiles(x86) }" # fail (2), LINE 6
# $d = "$Env:ProgramFiles(x86)" # fail (1)
if ( $d -eq $ok ) {
Write-Output "Pass"
} elseif ( $d -eq $bad ) {
Write-Output "Fail: (1) %ProgramFiles% used instead of %ProgramFiles(x86)%"
} else {
Write-Output "Fail: (2) some other reason"
}
}
这是输出:
PS> Do-Test
Fail: (2) some other reason
我可以对上面的第 6 行进行简单的更改以获得 %ProgramFiles(x86)% 的正确值吗?
注意:在本文中,我使用环境变量的批处理文件语法作为方便的简写。例如 %SOME_VARIABLE% 表示“名称为 SOME_VARIABLE 的环境变量的值”。如果我知道 PowerShell 中正确转义的语法,我就不需要问这个问题。
In a PowerShell script, how does one get the value of an environment variable whose name contains parentheses?
To complicate matters, some variables' names contains parentheses while others have similar names without parentheses. For example (using cmd.exe
):
C:\>set | find "ProgramFiles"
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
We see that %ProgramFiles%
is not the same as %ProgramFiles(x86)%
.
My PowerShell code is failing in a weird way because it's ignoring the part of the environment variable name after the parentheses. Since this happens to match the name of a different, but existing, environment variable I don't fail, I just get the right value of the wrong variable.
Here's a test function in the PowerShell scripting language to illustrate my problem:
function Do-Test
{
$ok = "C:\Program Files (x86)" # note space between 's' and '('
$bad = "$Env:ProgramFiles" + "(x86)" # uses %ProgramFiles%
$d = "${ Env:ProgramFiles(x86) }" # fail (2), LINE 6
# $d = "$Env:ProgramFiles(x86)" # fail (1)
if ( $d -eq $ok ) {
Write-Output "Pass"
} elseif ( $d -eq $bad ) {
Write-Output "Fail: (1) %ProgramFiles% used instead of %ProgramFiles(x86)%"
} else {
Write-Output "Fail: (2) some other reason"
}
}
And here's the output:
PS> Do-Test
Fail: (2) some other reason
Is there a simple change I can make to line 6 above to get the correct value of %ProgramFiles(x86)%?
NOTE: In the text of this post I am using batch file syntax for environment variables as a convenient shorthand. For example %SOME_VARIABLE% means "the value of the environment variable whose name is SOME_VARIABLE". If I knew the properly escaped syntax in PowerShell, I wouldn't need to ask this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的。更改第 6 行以删除括号内的空格:
您只需将包含 () 的变量用 {} 括起来即可。括号内没有空格。
Simple. Change line 6 to remove the spaces inside the brackets:
You just have to wrap the variable that contains () with {}. No spaces inside the brackets.