繁体   English   中英

如何在 Windows 上使用 AWS-RunPowerShellScript 运行包含空格的命令?

[英]How can I use AWS-RunPowerShellScript on Windows to run a command that contains a space?

我正在尝试使用来自某些 Java 代码的 AWS Systems Manager (SSM) 在 Windows 上运行程序。 这是代码:

String commands = "C:\\Program Files\\MyProgram.exe --key1=value1";
SendCommandRequest commandRequest = new SendCommandRequest()
        .withDocumentName( "AWS-RunPowerShellScript" )
        .withInstanceIds( instanceId )
        .addParametersEntry( "commands", Collections.singletonList( commands ) );

我得到的错误是:

C:\Program : The term 'C:\Program' is not recognized as the name of a cmdlet, 
function, script file, or operable program. Check the spelling of the name, or 
if a path was included, verify that the path is correct and try again.

我尝试在命令名称周围加上单引号,如下所示:

String commands = "'C:\\Program Files\\MyProgram.exe' --key1=value1";
SendCommandRequest commandRequest = new SendCommandRequest()
        .withDocumentName( "AWS-RunPowerShellScript" )
        .withInstanceIds( instanceId )
        .addParametersEntry( "commands", Collections.singletonList( commands ) );

但后来我得到这个错误:

+ 'C:\Program Files\MyProgram.exe' --key1=value1
+                                    ~~~~~~~~~~~
Unexpected token 'key1=value1' in expression or statement.
At C:\ProgramData\Amazon\SSM\InstanceData\i-0ad13762af97f97f1\document\orchestr
ation\784e0a71-dc7d-4a9e-944c-8820a7db6530\awsrunPowerShellScript\0.awsrunPower
ShellScript\_script.ps1:1 char:1
+ 'C:\Program Files\MyProgram.exe' --key1=value1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The '--' operator works only on variables or on properties.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx 
   ception
    + FullyQualifiedErrorId : UnexpectedToken

如何运行包含空格的命令,并将其与命令行 arguments 结合使用?

您必须正确转义双引号。 这应该有效:

String commands = "\"C:\\Program Files\\MyProgram.exe --key1=value1\"";

我无法让 Paulo 的建议起作用(我不再收到错误消息),但我使用不同的方法解决了它:

String commands = "cmd /C 'C:\\Program Files\\MyProgram.exe --key1=value1'";

暂无
暂无

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

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