首页 > 解决方案 > 如何在 Windows 上使用 AWS-RunPowerShellScript 运行包含空格的命令?

问题描述

我正在尝试使用某些 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

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

标签: windowsamazon-web-servicespowershellssm

解决方案


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

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

推荐阅读