简体   繁体   English

带有空格和大括号的Powershell命令行参数?

[英]Powershell command line argument with spaces and curly brackets?

I am making an attempt at my first powershell script and getting a bad parameter error when running the following code. 我正在尝试第一个Powershell脚本,并在运行以下代码时遇到错误的参数错误。 How can I pass the argument to the command in powersehll? 如何在powersehll中将参数传递给命令?

& "bcdedit" /store c:\boot\bcd /set {bootmgr} device partition=C:

EDIT: The working code for this is: 编辑:为此的工作代码是:

& "bcdedit" /store c:\boot\bcd /set "{bootmgr}" device partition=C:

The curly brackets threw everything off. 大括号把所有东西都扔掉了。 Putting quotes around {bootmgr} fixed the problem. {bootmgr}周围加上引号可以解决此问题。

& "bcdedit" /store c:\boot\bcd /set "{bootmgr}" device partition=C:

The problem you are running into is that the PowerShell parser works differently than the cmd.exe parser does. 您遇到的问题是PowerShell解析器的工作方式与cmd.exe解析器的工作方式不同。 One way around this is to pass your command to cmd.exe and let it do the parsing. 解决此问题的一种方法是将您的命令传递给cmd.exe然后让它进行解析。

To do this, pass the command to cmd.exe using the /c option as a single-quoted string. 为此,使用/c选项作为单引号字符串将命令传递到cmd.exe

cmd.exe /c 'bcdedit /store c:\boot\bcd /set {bootmgr} device partition=C:'

This method is especially useful when the command you are using requires string-quoted arguments. 当您使用的命令需要使用字符串引用的参数时,此方法特别有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM