SSM文档PowerShell脚本中布尔类型参数的正确引用方式
SSM文档PowerShell步骤引用布尔类型参数报错修复
问题场景
创建包含布尔类型参数的SSM文档时,执行文档更新操作抛出参数类型校验错误,相关配置、代码与报错信息如下:
1. 参数块配置
"createfoo":{ "type": "Boolean", "description": "Do you want to Create foo", "default": false }
2. runCommand执行块配置
"mainSteps": [ { "action": "aws:runPowerShellScript", "name": "InstallGAIN", "inputs":{ "runCommand": [ "[bool] $createfoo={{createfoo}}", "if ($createfoo -eq $true) {", "Write-Host \"Creating foo\"", "}" ] } } ]
3. SSM文档更新执行命令
$content = Get-Content -Path "installFoo.ssm" | Out-String $latestDocVersion = Update-SSMDocument ` -Content $content ` -Name "installFoo" ` -DocumentFormat "JSON" ` -DocumentVersion '$LATEST' ` | Select-Object -ExpandProperty LatestVersion Update-SSMDocumentDefaultVersion ` -Name "installFoo" ` -DocumentVersion $latestDocVersion
报错信息
Update-SSMDocument : Parameter "createfoo" is "BOOLEAN" type and can't be used as a substring parameter.
At line:2 char:21
- $latestDocVersion = Update-SSMDocument
- CategoryInfo : InvalidOperation: (Amazon.PowerShe...MDocumentCmdlet:UpdateSSMDocumentCmdlet) [Update-SSMDocument], InvalidOperationException
- FullyQualifiedErrorId : Amazon.SimpleSystemsManagement.Model.InvalidDocumentContentException,Amazon.PowerShell.Cmdlets.SSM.UpdateSSMDocumentCmdlet
报错原因
SSM参数校验规则要求:布尔类型参数不支持作为字符串子串嵌入命令,必须作为完整的独立值注入。原有配置中[bool] $createfoo={{createfoo}}的写法,没有给参数占位符加引号包裹,SSM会判定为将布尔参数拼接在字符串片段中使用,触发类型校验报错。
正确引用方式与修复方案
- 给布尔参数的
{{参数名}}占位符外层包裹单引号,让SSM识别为完整值注入,而非子串拼接 - 修复后的runCommand代码段如下:
"runCommand": [ "[bool] $createfoo = '{{createfoo}}'", "if ($createfoo -eq $true) {", "Write-Host \"Creating foo\"", "}" ]
说明:SSM执行时会自动将布尔参数替换为True/False字符串,PowerShell可直接识别转换为布尔类型,无需额外做特殊类型处理。
- 替换原有配置后重新执行文档更新命令,即可正常完成SSM文档提交,不会再触发该类型校验错误。
内容的提问来源于stack exchange,提问作者Shobit Mahajan
相关产品推荐
相关产品推荐

