如何在PowerShell中使用带有空格和引号的参数运行EXE文件

您所在的位置:网站首页 文件名能不能包含空格 如何在PowerShell中使用带有空格和引号的参数运行EXE文件

如何在PowerShell中使用带有空格和引号的参数运行EXE文件

#如何在PowerShell中使用带有空格和引号的参数运行EXE文件| 来源: 网络整理| 查看: 265

本文翻译自:How to run an EXE file in PowerShell with parameters with spaces and quotes

How do you run the following command in PowerShell? 如何在PowerShell中运行以下命令?

C:\\Program Files\\IIS\\Microsoft Web Deploy\\msdeploy.exe -verb:sync -source:dbfullsql="Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;" 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;",computername=10.10.10.10,username=administrator,password=adminpass" -dest:dbfullsql =“ Data Source =。\\ mydestsource; Integrated Security = false; User ID = sa; Pwd = sapass!; Database = mydb;”,计算机名= 10.10.10.10,用户名= administrator,密码= adminpass“

#1楼

参考:https://stackoom.com/question/71TT/如何在PowerShell中使用带有空格和引号的参数运行EXE文件

#2楼

See this page: https://slai.github.io/posts/powershell-and-external-commands-done-right/ 看到此页面: https : //slai.github.io/posts/powershell-and-external-commands-done-right/

Summary using vshadow as the external executable: 使用vshadow作为外部可执行文件的摘要:

$exe = "H:\backup\scripts\vshadow.exe" &$exe -p -script=H:\backup\scripts\vss.cmd E: M: P: #3楼

New escape string in PowerShell V3, quoted from New V3 Language Features : PowerShell V3中的新转义字符串,引用自V3新语言功能 :

Easier Reuse of Command Lines From Cmd.exe 从Cmd.exe更轻松地重用命令行

The web is full of command lines written for Cmd.exe. Web上充满了为Cmd.exe编写的命令行。 These commands lines work often enough in PowerShell, but when they include certain characters, for example, a semicolon (;), a dollar sign ($), or curly braces, you have to make some changes, probably adding some quotes. 这些命令行在PowerShell中通常可以正常工作,但是当它们包含某些字符(例如,分号(;),美元符号($)或花括号)时,您必须进行一些更改,可能添加一些引号。 This seemed to be the source of many minor headaches. 这似乎是许多轻微头痛的根源。

To help address this scenario, we added a new way to “escape” the parsing of command lines. 为了帮助解决这种情况,我们添加了一种新的方法来“逃避”命令行的解析。 If you use a magic parameter --%, we stop our normal parsing of your command line and switch to something much simpler. 如果您使用魔术参数-%,我们将停止对命令行的常规分析,并切换到更简单的命令。 We don't match quotes. 我们不匹配报价。 We don't stop at semicolon. 我们不止于分号。 We don't expand PowerShell variables. 我们不扩展PowerShell变量。 We do expand environment variables if you use Cmd.exe syntax (eg %TEMP%). 如果您使用Cmd.exe语法(例如%TEMP%),我们会扩展环境变量。 Other than that, the arguments up to the end of the line (or pipe, if you are piping) are passed as is. 除此之外,直到行(或管道,如果您是管道)末尾的参数都按原样传递。 Here is an example: 这是一个例子:

PS> echoargs.exe --% %USERNAME%,this=$something{weird} Arg 0 is #4楼

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: 当PowerShell看到以字符串开头的命令时,它只会对字符串求值,也就是说,它通常会将其回显到屏幕,例如:

PS> "Hello World" Hello World

If you want PowerShell to interpret the string as a command name then use the call operator (&) like so: 如果您希望PowerShell将字符串解释为命令名称,请使用调用运算符(&),如下所示:

PS> & 'C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.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. 当您使用复杂的命令行参数调用像这样的EXE文件时,拥有一个向您展示PowerShell如何将参数发送到EXE文件的工具通常非常有帮助。 The PowerShell Community Extensions has such a tool. PowerShell社区扩展具有此类工具。 It is called echoargs. 它称为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: 您只需用echoargs替换EXE文件-保留所有参数,这将向您展示EXE文件如何接收参数,例如:

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 Arg 1 is Arg 2 is Arg 3 is Arg 4 is Arg 5 is Arg 6 is Arg 7 is Arg 8 is

Using echoargs you can experiment until you get it right, for example: 使用echoargs可以尝试直到正确为止,例如:

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

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. 显然这不是必需的,因为即使cmd.exe也会将其删除。

BTW, hats off to the PowerShell team. 顺便说一句,向PowerShell团队致敬。 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 Bug 。

For more information on how PowerShell parses, check out my Effective PowerShell blog series - specifically item 10 - "Understanding PowerShell Parsing Modes" 有关PowerShell如何解析的更多信息,请查看我的有效PowerShell博客系列 -特别是项目10-“了解PowerShell解析模式”

UPDATE 4/4/2012: This situation gets much easier to handle in PowerShell V3. 2012年4月4日更新:在PowerShell V3中更容易处理这种情况。 See this blog post for details . 有关详细信息,请参见此博客文章 。

#5楼

This worked for me: 这对我有用:

& 'D:\Server\PSTools\PsExec.exe' @('\\1.1.1.1', '-accepteula', '-d', '-i', $id, '-h', '-u', 'domain\user', '-p', 'password', '-w', 'C:\path\to\the\app', 'java', '-jar', 'app.jar')

Just put paths or connection strings in one array item and split the other things in one array item each. 只需将路径或连接字符串放在一个数组项中,然后将其他内容拆分到一个数组项中。

There are a lot of other options here: https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx 这里还有很多其他选项: https : //social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

Microsoft should make this way simpler and compatible with command prompt syntax. Microsoft应该使这种方法更简单并且与命令提示符语法兼容。

#6楼

I had spaces in both command and parameters, and this is what worked for me: 我在命令和参数中都有空格,这对我有用:

$Command = "E:\X64\Xendesktop Setup\XenDesktopServerSetup.exe" $Parms = "/COMPONENTS CONTROLLER,DESKTOPSTUDIO,DESKTOPDIRECTOR,LICENSESERVER,STOREFRONT /PASSIVE /NOREBOOT /CONFIGURE_FIREWALL /NOSQL" $Prms = $Parms.Split(" ") & "$Command" $Prms

It's basically the same as Akira's answer, but this works if you dynamically build your command parameters and put them in a variable. 它与Akira的答案基本相同,但是如果您动态构建命令参数并将其放在变量中,则此方法有效。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3