【问题标题】:What is the correct way to reference a boolean parameter in SSM document for powershell?在 Powershell 的 SSM 文档中引用布尔参数的正确方法是什么?
【发布时间】:2022-06-13 13:47:01
【问题描述】:

我正在尝试创建一个处理参数的 SSM 文档,但我得到了

这是我的参数块:

        "createfoo":{
            "type": "Boolean",
            "description": "Do you want to Create foo",
            "default": false
        }

这是我的 runCommand 块:

        "mainSteps": [
            {
                "action": "aws:runPowerShellScript",
                "name": "InstallGAIN",
                "inputs":{
                    "runCommand": [ 
                        "[bool] $createfoo={{createfoo}}",
                        "if ($createfoo -eq $true) {",
                        "Write-Host \"Creating foo\"",
                         "}"

] } ]

Update-SSMDocument : 参数“createfoo”是“BOOLEAN”类型,不能用作 子字符串参数。 在 line:2 char:21

  • $latestDocVersion = 更新-SSMDocument
    • CategoryInfo : InvalidOperation: (Amazon.PowerShe...MDocumentCmdlet:UpdateSSMDo
      cumentCmdlet) [更新-SSMDocument],InvalidOperationException + FullyQualifiedErrorId : Amazon.SimpleSystemsManagement.Model.InvalidDocumentContentExcep
      化,Amazon.PowerShell.Cmdlets.SSM.UpdateSSMDocumentCmdlet

以下是我运行以更新我的文档的命令

$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

【问题讨论】:

  • @mklement0 谢谢,问题不在于我更新 SSM 文档的方式,而在于我如何尝试在 runco​​mmand 块中使用参数的 (createfoo) 值 (false)。
  • 根据错误消息和浏览文档(我不熟悉 AWS Systems Manager),看起来不允许在字符串中嵌入布尔参数,但您需要通过PowerShell 代码作为单个字符串(对吗?)。也许您可以将参数定义为 string。请注意,即使允许在 [bool] $createfoo={{createfoo}} 中扩展 createFoo,它也不会在 PowerShell 中工作,因为像 [bool] $createfoo=true 这样的东西会寻找 true 作为 命令; 01 可以工作
  • 如果{{createfoo}} 扩展为truefalse 的字符串值,则必须使用[bool] $createfoo= if ('true' -eq '{{createfoo}}') { $true } else { $false } 之类的东西

标签: amazon-web-services powershell aws-ssm


【解决方案1】:

按照 mklment0 在 cmets 中的建议,我可以通过将 True/False 值作为字符串传递来做到这一点。 以下是参数块的样子:

        "createfoo":{
            "type": "String",
            "description": "Do you want to Create foo",
            "default": "false"
        }

还有runCommand 块:

        "mainSteps": [
            {
                "action": "aws:runPowerShellScript",
                "name": "InstallGAIN",
                "inputs":{
                    "runCommand": [ 
                        "if ({{createFoo}} -eq 'true') {",
                        "Write-Host \"Creating foo\"",
                         "}"

] } ]

【讨论】:

    猜你喜欢
    • 2013-03-02
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    相关资源
    最近更新 更多