在powershell中遍历json文件

p1tboqfb  于 2023-07-01  发布在  Shell
关注(0)|答案(1)|浏览(144)

在浏览了SO上的几个示例之后,我找不到一个足够接近的具有类似数据结构的示例。我有下面的数据结构。我正在寻找提取值为以下键在vnetArray

  • vNetName
  • subnetName
  • SubnetAddressSpace
  • 服务端点
  • 安全规则
  • 路线

对于subnetArray。我期待提取以下领域

  • vNetName
  • subnetName
  • SubnetAddressSpace
  • 服务端点
  • 安全规则
  • 路线
  • 代表团

根据我看到的一些例子,我写了下面不起作用的。

$a = Get-Content '.\json_file.json' | ConvertFrom-Json
foreach ($record in $a)
{
        write-host "$($record.vNetArray.vNetName) | $($record.vNetArray.vNetLocation) | $($record.vNetArray.NetworkType) | $($record.vNetArray.vNetAddressSpace)"
}

json_file.json

{
    "$Schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "$contentVersion": "1.0.0.0",
    "parameters": {
      "vNetArray": {
        "value": [
          {
            "vNetName": "vnet-test-cus-stg",
            "vNetLocation": "CentralUS",
            "NetworkType": "Spoke",
            "vNetAddressSpace": "10.1.0.0/16",
            "subnets": [
              {
                "vNetName": "vnet-test-cus-stg",
                "subnetName": "snet-webapp-cus-stg",
                "SubnetAddressSpace": "10.1.1.0/25",
                "serviceEndpoints": [],
                "securityRules": [],
                "routes": []
              },
             {
                "vNetName": "vnet-test-cus-stg",
                "subnetName": "snet-agw-cus-stg",
                "SubnetAddressSpace": "10.1.3.0/25",
                "serviceEndpoints": [],
                "securityRules": [],
                "routes": []
              },
              {
                "vNetName": "vnet-test-cus-stg",
                "subnetName": "snet-sql-cus-stg",
                "SubnetAddressSpace": "10.1.4.0/25",
                "serviceEndpoints": [],
                "securityRules": [],
                "routes": []
              }
            ]
          }
        ]
      },
      "subnetArray": {
        "value": [
          {
            "vNetName": "vnet-test-cus-stg",
            "subnetName": "snet-webapp-cus-stg",
            "SubnetAddressSpace": "10.1.1.0/25",
            "serviceEndpoints": [],
            "securityRules": [],
            "routes": [],
            "delegations":[
              {
                  "name": "Microsoft.Web.serverFarms",
                  "properties": {
                      "serviceName": "Microsoft.Web/serverFarms"
                  }
              } 
          ],
          "PSDelegation": "Microsoft.Web/serverFarms"       
          },
          {
            "vNetName": "vnet-test-cus-stg",
            "subnetName": "snet-agw-cus-stg",
            "SubnetAddressSpace": "10.1.3.0/25",
            "serviceEndpoints": [],
            "securityRules": [],
            "routes": [],
            "delegations":[],
            "PSDelegation": ""                   
          },
          {
            "vNetName": "vnet-test-cus-stg",
            "subnetName": "snet-sql-cus-stg",
            "SubnetAddressSpace": "10.1.4.0/25",
            "serviceEndpoints": [
              {
                "service": "Microsoft.sql"
              }
            ],
            "securityRules": [],
            "routes": [],
            "delegations":[],     
            "PSDelegation": "Microsoft.Sql/managedInstances"       
          }
        ]
      }
    }
  }

我为什么要这么做
有一个限制/bicep内可能的错误,和一种方法,我已经测试了绕过这个问题是改变一个子网通过AZ CLI通过运行下面的代码.

az network vnet subnet update \
    --resource-group myResourceGroup \
    --name mySubnet \
    --vnet-name myVNet \
    --delegations Microsoft.Sql/managedInstances

流程将像这样工作。
1.写一个Ps文件,它接受一个资源组作为参数。
1.检查资源组是否存在,如果继续。如果它不抛出错误。
1.在json文件中查找具有PSDelegation值的数组部分如果有值检查上面循环中的vnet是否存在于RG(资源组)中
1.如果vnet存在,请检查子网是否存在,如果不存在,则继续执行,如果不存在,则抛出错误。
1.现在通过运行下面的代码片段编辑子网。
az网络vnet子网更新
--resource-group myResourceGroup
--name mySubnet
--vnet-name myVNet
--委派Microsoft.Sql/managedInstances

wnrlj8wa

wnrlj8wa1#

根据评论和OP编辑:

一旦你有了json,你就可以使用value访问subnetArray,然后像你期望的那样访问$record中的字段。

# parameter declaration, resource group declaration...
# check for existence also goes here
$myResourceGroup = 'foo'

# get your json and make sure vNet+subnet exist
$json = Get-Content '.\json_file.json' -Raw | ConvertFrom-Json
if ($json.parameters.psobject.properties.name -contains 'vNetArray' -and -not $json.parameters.psobject.properties.name -contains 'subnetArray'){
    throw 'subnetArray not found!'
}

# edit the subnet 
foreach ($record in $json.parameters.subnetArray.value) {
    if ($record.PSDelegation){
      $mySubnet = $record.subnetName
      $myVNet = $record.vNetName
      & az network vnet subnet update --resource-group $myResourceGroup --name $mySubnet --vnet-name $myVNet --delegations Microsoft.Sql/managedInstances
    }
}

相关问题