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

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

问题描述

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

How do you run the following command in PowerShell?

C:\ Program Files \ IIS \ Microsoft Web Deploy \ msdeploy.exe -verb:sync -source:dbfullsql =数据源= mysource;集成安全性= false;用户ID = sa; Pwd = sapass !;数据库= mydb;"-dest:dbfullsql ="Data Source =.\ mydestsource; Integrated Security = false; User ID = sa; Pwd = sapass!; Database = mydb;",计算机名= 10.10.10.10,用户名= administrator,密码= 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 Files\IIS\Microsoft Web Deploy\msdeploy.exe'

之后,您可能只需要引用包含空格和/或引号字符的参数/参数对.当您使用复杂的命令行参数调用像这样的EXE文件时,拥有一个将向您展示PowerShell如何将参数发送到EXE文件的工具通常非常有帮助. PowerShell社区扩展有这样的工具.它称为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团队致敬.他们在向我展示单人&的具体咒语方面非常有帮助.双引号以获得所需的结果-如果需要将内部双引号保留在适当的位置. :-)他们也意识到这是一个痛苦的领域,但是受特定问题影响的人数驱动.如果这是让您感到痛苦的地方,请对 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博客系列-特别是

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

2012年4月4日更新:这种情况在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天全站免登陆