在 PowerShell 中运行命令行 [英] Run command line in PowerShell

查看:156
本文介绍了在 PowerShell 中运行命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于此的帖子,但对我没有任何效果.

I know there are lots of posts regarding this, but nothing worked for me.

我正在尝试在 PowerShell 中运行此命令行:

I am trying to run this command line in PowerShell:

C:/Program Files (x86)/ClamWin/bin/clamd.exe --install

我在 PowerShell 中有这个:

I have this in PowerShell:

&"C:/Program Files (x86)/ClamWin/bin/clamd.exe --install"

但这只是执行clamd.exe,忽略--install参数

But all this does is execute clamd.exe, ignoring the --install parameter

如何让完整的命令行运行?

How can I get the full command line to run?

推荐答案

Josef Z's 对问题的评论提供了解决方案:

Josef Z's comment on the question provides the solution:

& "C:/Program Files (x86)/ClamWin/bin/clamd.exe" --install # double-quoted exe path

或者,假设可执行路径是一个文字(不包含变量引用或子表达式):

or, given that the executable path is a literal (contains no variable references or subexpressions):

& 'C:/Program Files (x86)/ClamWin/bin/clamd.exe' --install # single-quoted exe path

至于为什么您自己的解决方案尝试失败:调用运算符 & 只需要 命令名称/路径 作为参数,而不是完整的 命令行.
Invoke-Expression 接受整个命令行,但这会使事情进一步复杂化,并且可能是 安全风险.

As for why your own solution attempt failed: The call operator & expects only a command name/path as an argument, not a full command line.
Invoke-Expression accepts an entire command line, but that complicates things further and can be a security risk.

至于为什么这是解决方案:

  • 需要引用是有道理的:您需要告诉 PowerShell C:/Program Files (x86)/ClamWin/bin/clamd.exe 是一个单个标记(路径),尽管包含嵌入的空格.

  • The need for quoting stands to reason: you need to tell PowerShell that C:/Program Files (x86)/ClamWin/bin/clamd.exe is a single token (path), despite containing embedded spaces.

&,即所谓的调用运算符,是必需的,因为PowerShell 有两种基本的解析模式:

&, the so-called call operator, is needed, because PowerShell has two fundamental parsing modes:

  • 参数模式工作方式类似于传统 shell,其中第一个标记是命令名称,随后的标记表示参数,如果它们包含 shell 元字符(字符.对 PowerShell 具有特殊含义,例如空格分隔标记),则只需要引用;
    这就是为什么--install 不需要,但可以被引用(PowerShell会在将参数传递给目标之前简单地删除引号)可执行文件.)

  • argument mode, which works like a traditional shell, where the first token is a command name, with subsequent tokens representing the arguments, which only require quoting if they contain shell metacharacters (chars. with special meaning to PowerShell, such as spaces to separate tokens);
    that is why --install need not, but can be quoted (PowerShell will simply remove the quotes for you before passing the argument to the target executable.)

表达式模式,它的工作方式类似于编程语言中的表达式.

expression mode, which works like expressions in programming languages.

PowerShell 根据语句的 first 标记决定应用哪种解析模式:

PowerShell decides based on a statement's first token what parsing mode to apply:

如果第一个标记是一个带引号的字符串 - 由于可执行路径中的嵌入空格,我们在这里需要 - 变量引用(例如,$var ...),PowerShell 默认以表达式模式解析.
带引号的字符串或变量引用作为表达式将简单地输出字符串/变量值.

If the first token is a quoted string - which we need here due to the embedded spaces in the executable path - or a variable reference (e.g., $var ...), PowerShell parses in expression mode by default.
A quoted string or a variable reference as an expression would simply output the string / variable value.

然而,鉴于我们要执行路径存储在带引号的字符串中的可执行文件,我们需要强制参数模式,这就是& 运算符确保.

However, given that we want to execute the executable whose path is stored in a quoted string, we need to force argument mode, which is what the & operator ensures.

通常,重要的是要了解 PowerShell 在调用目标可执行文件之前对命令行执行非平凡的预处理,因此命令行在 PowerShell 代码中的是什么样子 通常不是直接目标可执行文件看到的.

Generally, it's important to understand that PowerShell performs nontrivial pre-processing of the command line before the target executable is invoked, so what the command line looks like in PowerShell code is generally not directly what the target executable sees.

  • 如果您在命令行上引用了 PowerShell 变量,并且该变量包含嵌入的空格,PowerShell 将隐式地将变量的值括在双引号 在传递之前 - 这在这个答案中讨论到链接问题.

  • If you reference a PowerShell variable on the command line and that variable contains embedded spaces, PowerShell will implicitly enclose the variable's value in double quotes before passing it on - this is discussed in this answer to the linked question.

PowerShell 的元字符cmd.exe 不同,而且数量更多(特别是 在 PowerShell(数组构造函数)中具有特殊含义,但在 cmd.exe 中没有特殊含义 - 请参阅此答案).

PowerShell's metacharacters differ from that of cmd.exe and are more numerous (notably, , has special meaning in PowerShell (array constructor), but not cmd.exe - see this answer).

为了简化现有的基于 cmd.exe 的命令行的重用,PowerShell v3 引入了特殊的停止解析符号--%,关闭 PowerShell 对命令行其余部分的正常解析,只插入 cmd.exe 风格的环境变量引用(例如,%USERNAME%).

To simplify reuse of existing, cmd.exe-based command lines, PowerShell v3 introduced the special stop-parsing symbol, --%, which turns off PowerShell's normal parsing of the remainder of the command line and only interpolates cmd.exe-style environment-variable references (e.g., %USERNAME%).

这篇关于在 PowerShell 中运行命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆