如何在 PowerShell 中运行带有空格和引号的参数的 EXE 文件 [英] How to run an EXE file in PowerShell with parameters with spaces and quotes

查看:23
本文介绍了如何在 PowerShell 中运行带有空格和引号的参数的 EXE 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 PowerShell 中运行以下命令?

How do you run the following command in PowerShell?

C:Program FilesIISMicrosoft Web Deploymsdeploy.exe -verb:sync -source:dbfullsql="Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;"-dest:dbfullsql="Data Source=.mydestsource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;",computername=10.10.10.10,username=administrator,password=adminpass"

C:Program FilesIISMicrosoft Web Deploymsdeploy.exe -verb:sync -source:dbfullsql="Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;" -dest:dbfullsql="Data Source=.mydestsource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;",computername=10.10.10.10,username=administrator,password=adminpass"

推荐答案

当 PowerShell 看到一个以字符串开头的命令时,它只会评估该字符串,也就是说,它通常会将其回显到屏幕上,例如:

When PowerShell sees a command starting with a string it just evaluates the string, that is, it typically echos it to the screen, for example:

PS> "Hello World"
Hello World

如果您希望 PowerShell 将字符串解释为命令名称,请使用调用运算符 (&),如下所示:

If you want PowerShell to interpret the string as a command name then use the call operator (&) like so:

PS> & 'C:Program FilesIISMicrosoft Web Deploymsdeploy.exe'

之后,您可能只需要引用包含空格和/或引号字符的参数/参数对.当您使用复杂的命令行参数调用这样的 EXE 文件时,拥有一个工具通常会向您展示 PowerShell 如何将参数发送到 EXE 文件.PowerShell Community Extensions 有这样一个工具.它被称为 echoargs.您只需用 echoargs 替换 EXE 文件 - 保留所有参数,它将向您展示 EXE 文件将如何接收参数,例如:

After that you probably only need to quote parameter/argument pairs that contain spaces and/or quotation chars. When you invoke an EXE file like this with complex command line arguments it is usually very helpful to have a tool that will show you how PowerShell sends the arguments to the EXE file. The PowerShell Community Extensions has such a tool. It is called echoargs. You just replace the EXE file with echoargs - leaving all the arguments in place, and it will show you how the EXE file will receive the arguments, for example:

PS> echoargs -verb:sync -source:dbfullsql="Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;" -dest:dbfullsql="Data Source=.mydestsource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;",computername=10.10.10.10,username=administrator,password=adminpass

Arg 0 is <-verb:sync>
Arg 1 is <-source:dbfullsql=Data>
Arg 2 is <Source=mysource;Integrated>
Arg 3 is <Security=false;User>
Arg 4 is <ID=sa;Pwd=sapass!;Database=mydb;>
Arg 5 is <-dest:dbfullsql=Data>
Arg 6 is <Source=.mydestsource;Integrated>
Arg 7 is <Security=false;User>
Arg 8 is <ID=sa;Pwd=sapass!;Database=mydb; computername=10.10.10.10 username=administrator password=adminpass>

您可以使用 echoargs 进行实验,直到正确为止,例如:

Using echoargs you can experiment until you get it right, for example:

PS> echoargs -verb:sync "-source:dbfullsql=Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;"
Arg 0 is <-verb:sync>
Arg 1 is <-source:dbfullsql=Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;>

事实证明,我之前太努力地在连接字符串周围保留双引号.显然这不是必需的,因为即使是 cmd.exe 也会把它们去掉.

It turns out I was trying too hard before to maintain the double quotes around the connection string. Apparently that isn't necessary because even cmd.exe will strip those out.

顺便说一句,向 PowerShell 团队致敬.他们向我展示了 single & 的特定咒语非常有帮助.双引号以获得所需的结果 - 如果您需要保留内部双引号.:-) 他们也意识到这是一个痛苦的领域,但他们受到受特定问题影响的人数的推动.如果这对你来说是一个痛苦的领域,那么请投票支持这个 PowerShell 错误提交.

BTW, hats off to the PowerShell team. They were quite helpful in showing me the specific incantation of single & double quotes to get the desired result - if you needed to keep the internal double quotes in place. :-) They also realize this is an area of pain, but they are driven by the number of folks are affected by a particular issue. If this is an area of pain for you, then please vote up this PowerShell bug submission.

有关 PowerShell 如何解析的更多信息,请查看我的 有效的 PowerShell 博客系列 - 特别是 第 10 项 - 了解 PowerShell 解析模式"

For more information on how PowerShell parses, check out my Effective PowerShell blog series - specifically item 10 - "Understanding PowerShell Parsing Modes"

更新 4/4/2012:这种情况在 PowerShell V3 中更容易处理.请参阅此 博客文章详情.

UPDATE 4/4/2012: This situation gets much easier to handle in PowerShell V3. See this blog post for details.

这篇关于如何在 PowerShell 中运行带有空格和引号的参数的 EXE 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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