AWS-RunPowerShellScript CLI执行Windows命令时无法识别大括号
解决AWS CLI Send-Command大括号识别问题
问题分析
你的命令里的大括号{}被shell(如bash)解析成了大括号扩展语法,而非传递给PowerShell执行;同时PowerShell的引号、变量转义和AWS CLI的参数格式存在冲突,导致命令无法正确识别。
解决方案
方案1:使用完整JSON格式传递参数(推荐)
用JSON结构明确定义参数,避免shell解析特殊字符,转义逻辑更清晰:
aws ssm --region us-east-1 send-command \ --instance-ids "i-008d4b86c352c2bcf" \ --document-name "AWS-RunPowerShellScript" \ --parameters '{ "commands": [ "$MyApp = Get-CimInstance -ClassName Win32_Product | Where-Object {$_.Name -eq \"App I do not want\"}", "Start-Sleep 600", "$MyApp.Uninstall()" ] }'
- 用单引号包裹整个JSON字符串,确保shell不解析内部的
$和{} - JSON字符串内的双引号用
\"转义,符合JSON语法要求 - 替换
Get-WmiObject为Get-CimInstance(Win32_Product搭配WMI可能触发MSI自我修复,CIM是更安全的替代)
方案2:转义shell特殊字符(key=value格式)
如果坚持用简化的key=value格式,需要对shell会解析的$、{}进行转义,同时调整PowerShell的引号用法:
aws ssm --region us-east-1 send-command \ --instance-ids "i-008d4b86c352c2bcf" \ --document-name "AWS-RunPowerShellScript" \ --parameters "commands=[\"\$MyApp = Get-CimInstance -ClassName Win32_Product | Where-Object\{\$_.Name -eq 'App I do not want'\}\", \"Start-Sleep 600\", \"\$MyApp.Uninstall()\"]"
- 用双引号包裹整个
--parameters值,对shell变量$转义为\$ - 大括号
{}前加\转义,防止bash进行大括号扩展 - PowerShell内的字符串用单引号包裹,避免和外层双引号冲突
额外注意事项
- 避免使用
Win32_Product类:该类会触发已安装MSI程序的自我修复,可能导致意外的系统行为,建议改用Get-CimInstance -ClassName Win32_RegistryValue或Get-Package定位应用 - 测试命令时,可以先在本地PowerShell中验证脚本逻辑,再封装到AWS CLI命令中
内容的提问来源于stack exchange,提问作者Clint
相关产品推荐
相关产品推荐

