windows - 如何在 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
如何运行包含空格的命令,并将其与命令行参数结合使用?
解决方案
我无法让 Paulo 的建议起作用(我不再收到错误消息),但我使用不同的方法解决了它:
String commands = "cmd /C 'C:\\Program Files\\MyProgram.exe --key1=value1'";
推荐阅读
- c - 使用 __builtin_extract_return_addr() 函数查找 ret 指令的 RSP 值
- python - 如何在熊猫中搜索单独列的文本匹配?
- google-apps-script - LockService 的脚本未激活锁
- node.js - NodeJs 和 Keycloak 集成给出 403?
- firebase - 实时数据库事务最初是否也在 Cloud Functions 中给出了空参数?
- python - ValueError:无法将字符串转换为浮点数 - 但我没有尝试插入浮点数
- python - 在将图像上传到 s3 存储桶之前压缩图像
- kubernetes - Kubernetes仪表板访问具有相同令牌或配置文件的命名空间
- npm - 如何让 eslint-plugin-import 为我的 npm 项目(create-react-app)工作?
- javascript - Javascript面试题(mul函数)