无法在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
以下命令:
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.
有人可以向我解释吗?
推荐答案
要传递包含在 {...}
中的值作为 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中使用无引号时具有特殊含义的字符(它们包含一个 script块),在这种情况下恰好导致 {
和}
已删除。
引用可以防止这种情况。
{
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起可用 ,就是使用所谓的 停止解析符号,-%
,w hich照原样传递所有剩余参数,无需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的报价(escapi ng)要求。
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
仅扩展%...%
括起来的变量名(例如,%PATH%
),而 PowerShell要求$
前缀的变量名(例如$ 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中的文字字符串(内容按原样使用,不带插值),而'
对<$没有特殊含义完全是c $ c> 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.
要处理元字符作为 literals ,您有两个选择:
To treat metacharacters as literals, you have two options:
-
带引号的字符串 :
Enclose them in quoted strings:
- 两个
cmd.exe
和PowerShell:将它们包含在...
中(但可能还会对字符串中包含的任何变量引用/子表达式进行插值);例如,| 。
- 仅PowerShell:将它们包含在
'...'$中c $ c>;例如,
'|'
- 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.,'|'
转义它们单独 :
Escape them individually:
- 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.
- 这仅对<$ c的外部有效$ c> ... ,可悲的是,它不能转义
%
来抑制变量扩展-请参见完整答案的答案。
- 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
- 这在