繁体   English   中英

Powershell - 创建一个 json 文件

[英]Powershell - create a json file

我准备了 Cloud Formation 模板,json 格式,如下:

{
"AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "Baseline": {
            "Properties": {
            
                "ApprovalRules": {
                    "PatchRules": [
                        {
                            "PatchFilterGroup": {
                                "PatchFilters": [
                                    {
                                        "Values": [],
                                        "Key": ""
                                    },
                                    {
                                        "Values": [],
                                        "Key": ""
                                    },
                                    {
                                        "Values": [],
                                        "Key": ""
                                    }
                                ]
                            }

                        }
                    ]
                }
            }
        }
    }
}

我需要将内容注入到那个 json 文件中,所以准备了代码。 我需要注入两个数组: ValuesKey

function ObjectBuilder {

    param (
    $Values,
    $Key
    )

    $counter = 0
    $objecttoinjectArray = @()
    foreach ($element1 in $Values) { 
            $objecttoinject = [pscustomobject][ordered] @{
                            Values = $element1
                            Key = $Key[$counter]
            }
            $objecttoinjectArray += $objecttoinject
            $counter++
        
    }
    return $objecttoinjectArray

}


$filename = "Matrix.json"
$content = Get-Content -Path .\$filename
$jsoncontent = $content | ConvertFrom-Json
$jsonbuild = $jsoncontent.Resources.Baseline.Properties
$Values = @("BMW, Audi", "Fiat, Ferrari, Porsche, Toyota", "*")
$Key = @("Standard","High","Premium")

$objecttoinjectArray = ObjectBuilder -Values $Values -Key $Key
$jsonbuild.ApprovalRules.PatchRules.PatchFilterGroup.PatchFilters = $objecttoinjectArray

$jsoncontent | 
ConvertTo-Json -Depth 15 | 
Set-Content .\tests.json

作为输出,我可以看到格式不正确的 json 文件,如下所示:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "Baseline": {
      "Properties": {
        "ApprovalRules": {
          "PatchRules": [
            {
              "PatchFilterGroup": {
                "PatchFilters": [
                  {
                    "Values": "BMW, Audi",
                    "Key": "Standard"
                  },
                  {
                    "Values": "Fiat, Ferrari, Porsche, Toyota",
                    "Key": "High"
                  },
                  {
                    "Values": "*",
                    "Key": "Premium"
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

正确的结构应如下所示,AWS cloudformation 不接受该结构:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "Baseline": {
      "Properties": {
        "ApprovalRules": {
          "PatchRules": [
            {
              "PatchFilterGroup": {
                "PatchFilters": [
                  {
                    "Values": [
            "BMW", 
            "Audi"
            ],
                    "Key": "Standard"
                  },
                  {
                    "Values": [
            "Fiat", 
            "Ferrari", 
            "Porsche", 
            "Toyota"
            ],
                    "Key": "High"
                  },
                  {
                    "Values": [
            "*"
            ],
                    "Key": "Premium"
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

这是因为$Values每个项目都是一个字符串,而不是一个数组( "BMW, Audi"而不是"BMW", "Audi" ),并且将被添加到 JSON 中。

根据您从何处获取这些值,有多种选择:

如果字符串来自文件,则必须将它们拆分:

$objecttoinject = [pscustomobject]@{
    # split up the string into an actual array of the values
    Values = $element1 -split ", "
    Key = $Key[$counter]
}

(顺便说一句,你可以省略[ordered]加速器,因为它已经被[pscustomobject]隐含了)

如果它们真的在您的脚本中真正定义了,也只需更改它:

$Values = @(@("BMW", "Audi"), @("Fiat", "Ferrari", "Porsche", "Toyota"), @("*"))

或者更好的是,使用哈希表:

$hashtable = @{
    Standard = "BMW", "Audi"
    High = "Fiat", "Ferrari", "Porsche", "Toyota"
    Premium = "*"
}
$objecttoinjectArray = $hashtable.GetEnumerator() | foreach {
    [pscustomobject] @{
        Values = @($_.Value)
        Key = $_.Key
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM