无法在 powershell 中使用 bcdedit filelds 进行编辑 - cmd.exe 命令行失败 [英] Unable to edit with bcdedit filelds in powershell - cmd.exe command line fails
问题描述
为什么我不能在 PowerShell 中使用 bcdedit
编辑描述字段?
Why can't I edit the description field using bcdedit
in PowerShell?
例如,在 cmd.exe
中的以下命令:
For example, in cmd.exe
the following command:
bcdedit /set {GUID} description "OS2"
成功完成,更改了指定 GUID 的描述字段,但是当我从 Powershell 执行相同操作时,出现以下错误:
completes successfully, changing the description field for the specified GUID, but when I do the same thing from Powershell, I get the following error:
The set command specified is not valid.
Run "bcdedit /?" for command line assistance.
The parameter is incorrect.
有人可以向我解释一下吗?
Can someone explain this to me?
推荐答案
将包含在 {...}
中的值作为 literal 传递(如-is) 在 PowerShell 中,您必须引用它;例如:
To pass a value enclosed in {...}
as a literal (as-is) in PowerShell, you must quote it; e.g.:
bcdedit /set "{340E0E1A-01EC-4A33-A850-8D6A09FD4CE9}" description "OS2"
{
和 }
与 cmd.exe
不同,它们是 元字符,即具有特殊性的字符在 PowerShell 中不加引号的意思(它们包含一个 脚本块),在这种情况下,这恰好导致 {
和 }
简单地获得 已删除.
引用可以防止这种情况.
{
and }
, unlike in cmd.exe
, are metacharacters, i.e., characters that have special meaning in PowerShell when used unquoted (they enclose a script block), which in this case happens to result in {
and }
simply getting removed.
Quoting prevents that.
或者,您可以`
-转义未加引号的元字符单独:bcdedit/set `{340E0E1A-01EC-4A33-A850-8D6A09FD4CE9`} 描述OS2"
Alternatively, you can `
-escape the unquoted metacharacters individually:
bcdedit /set `{340E0E1A-01EC-4A33-A850-8D6A09FD4CE9`} description "OS2"
自 PSv3 起可用的通用替代方案是使用所谓的停止解析符号,--%
,按原样传递所有剩余参数,没有 PowerShell 的解释(除了扩展 %...%
封闭的环境变量引用):
A generic alternative, available since PSv3, is to use the so-called stop-parsing symbol, --%
, which passes all remaining arguments as-is, without interpretation by PowerShell (with the exception of expanding %...%
-enclosed environment-variable references):
bcdedit --% /set {340E0E1A-01EC-4A33-A850-8D6A09FD4CE9} description "OS2"
警告:虽然 --%
如果您的所有参数都是文字,则按预期工作,但在您的情况下,一般情况下它会阻止您使用 PowerShell 变量 和 表达式 作为参数中的/,并且可能会产生其他意外的副作用 - 请参阅
Caveat: While --%
works as expected if all your arguments are literals, as in your case, in general it prevents you from using PowerShell variables and expressions as / in arguments, and can have other unexpected side effects - see this answer.
除非需要对 PowerShell 变量和表达式进行插值,否则 --%
允许按原样重复使用 cmd.exe
命令行,而无需担心PowerShell 的引用(转义)要求.
Unless interpolation of PowerShell variables and expression is needed, --%
allows reuse of cmd.exe
command lines as-is, without having to worry about PowerShell's quoting (escaping) requirements.
通常,PowerShell 的元字符(未加引号时具有特殊含义的字符)与 cmd.exe
不同,并且数量更多:
Generally, PowerShell's metacharacters (characters that have special meaning when unquoted) are different from cmd.exe
's and much more numerous:
除了 cmd.exe
的元字符,
& | < >
PowerShell 具有:
PowerShell has:
( ) , { } ; @ $ #
<
、>
、@
和 #
仅在 开始令牌.<
保留供未来使用,从 PowerShell 7.0 开始
<
, >
, @
and #
only have special meaning at the start of a token.
<
is reserved for future use, as of PowerShell 7.0
除此之外,关于变量扩展(插值):
cmd.exe
仅扩展%...%
-enclosed 变量名(例如,%PATH%
),而 PowerShell 需要$
-prefixed 变量名(例如,$env:PATH
或$HOME
) 或$(...)
封闭表达式(子表达式运算符)
cmd.exe
only expands%...%
-enclosed variable names (e.g.,%PATH%
), whereas PowerShell requires$
-prefixed variable names (e.g.,$env:PATH
or$HOME
) or$(...)
-enclosed expressions (subexpression operator)
- 在两个解释器中,变量扩展(以及在 PowerShell 中,还有子表达式扩展)也在
"..."
(双引号字符串)中执行.
- In both interpreters, variable expansion (and, in PowerShell, also subexpression expansion) is also performed inside
"..."
(double-quoted strings).
'...'
(单引号字符串)是 PowerShell 中的 文字 字符串(内容按原样使用,没有插值),而 '
对 cmd.exe
没有特殊意义.
'...'
(single-quoted strings) are literal strings in PowerShell (contents is used as-is, without interpolation), whereas '
has no special meaning to cmd.exe
at all.
要将元字符视为文字,您有两种选择:
To treat metacharacters as literals, you have two options:
将它们括在带引号的字符串中:
Enclose them in quoted strings:
cmd.exe
和 PowerShell:将它们包含在"..."
中(但可能会插入任何变量引用/子表达式也包含在字符串中);例如,"|"
.- 仅限 PowerShell:将它们括在
'...'
中;例如,'|'
- Both
cmd.exe
and PowerShell: enclose them in"..."
(but potentially with interpolation of any variable references / subexpressions also enclosed in the string); e.g.,"|"
. - PowerShell only: enclose them in
'...'
; e.g.,'|'
逃避他们单独:
- PowerShell:
`
-转义它们(反引号);例如,`|
- 这也适用于
"..."
,尽管它只需要转义$
以防止变量/子表达式扩展.
- PowerShell:
`
-escape them (backtick); e.g.,`|
- This also works inside
"..."
, although there it is only needed to escape$
so as to prevent variable / subexpression expansion.
- 这仅适用于
"..."
的外部,遗憾的是,它不适用于转义%
以抑制变量扩展 -请参阅此答案了解完整故事.
- This only works outside of
"..."
, and sadly, doesn't work for escaping%
to suppress variable expansion - see this answer for the full story.
这篇关于无法在 powershell 中使用 bcdedit filelds 进行编辑 - cmd.exe 命令行失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- This also works inside
- 这也适用于