Powershell 更新对象属性值不符合预期

问题描述 投票:0回答:1

我有一个 Json 文件,我只想更新一些指定的属性值,而不是 json 文件中的所有相同值。

但是目前的情况是我可以替换指定的属性值,但是它会多一个结构。

我的测试 Json 文件如下:

{
    "$schema":  "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion":  "1.0.0.0",
    "resources":  [
                      {
                          "apiVersion":  "2016-06-01",
                          "kind":  "V2",
                          "properties":  {
                                             "displayName":  "azuredatafactory",
                                             "overallStatus":  "Ready",
                                             "connectionState":  "Enabled",
                                             "customParameterValues":  {
                                                                       },
                                             "alternativeParameterValues":  {
                                                                            },
                                             "parameterValueType":  "Alternative",
                                             "createdTime":  "2024-06-27T09:03:39.9477Z",
                                             "changedTime":  "2024-06-27T09:03:39.9477Z",
                                             "testLinks":  [

                                                           ],
                                             "connectionRuntimeUrl":  "https://89ad7e55624ad1be.08.common.logic-eastasia.azure-apihub.net/apim/azuredatafactory/9fed9f23xxxxxa34f23d186b"
                                         },
                          "id":  "/subscriptions/e1c6546xxxxx218a/resourceGroups/rxxxxxl-ietl01/providers/Microsoft.Web/connections/azuredatafactory",
                          "name":  "azuredatafactory",
                          "type":  "Microsoft.Web/connections",
                          "location":  "eastasia",
                          "tags":  {
                                       "CreatedOnDate":  "2024-06-27T09:03:39.3814086Z",
                                       "Environment":  "D"
                                   }
                      }
                  ]
}

我只想更新节点

"displayName":  "azuredatafactory"
下的
resources.properties
和节点
"name":  "azuredatafactory"
下的
resources

我的测试powershell脚本如下所示:

$RevertTypeForJson = Get-Content -Path "<pathtoMyJsonfiles>\MyTestJson.json" -Raw | ConvertFrom-Json

$RevertTypeForJson.resources | ForEach-Object {
    $_.name = @($_.name -replace "$(VariableForAzuredatafactory)", "$(VariableForAzuredatafactory)_NewValue")
    $_.properties.displayName = @($_.properties.displayName -replace "$(VariableForAzuredatafactory)", "$(VariableForAzuredatafactory)_NewValue")
}

$NewRevertTypeForJson = $RevertTypeForJson | ConvertTo-Json -Depth 100
Write-Host "$NewRevertTypeForJson"

但是,给出的输出是:

                       "properties":  {
                                          "displayName":  [
                                                              "azuredatafactory_NewVlaue"

                                                          ],

                   "name":  [
                                "azuredatafactory_SIT"
                            ],

代替

"displayName": "azuredatafactory_NewVlaue",
"name":  "azuredatafactory_NewVlaue",

如何更新我的脚本以达到预期结果

"displayName": "azuredatafactory_NewVlaue",
"name":  "azuredatafactory_NewVlaue",

注意:我不能用全局变量替换。我知道这很简单,但是你可以看到 ID 值也包含相同的值,该值无法更改,所以我必须修改特定属性下的值。

json powershell
1个回答
1
投票

PowerShell 在输出类型方面通常非常灵活 - 如果管道输出 0 个对象,则任何分配目标都将收到

$null
,如果恰好有 1 个对象,您只会得到 1 个对象,如果有 2 个或更多 PowerShell 将换行数组中的输出。

这在大多数情况下效果很好,但有时您想要一个数组,无论特定管道或表达式计算的对象数量如何。

输入 数组子表达式运算符

@(...)

直接嵌套在

@(...)
中的任何管道或表达式都会导致 PowerShell always 将输出包装在数组中,这就是为什么您会在最终 JSON 中的值周围获得额外的
[...]
-
[...]
是 JSON 表示法对于数组。

删除

@(...)
,它将按预期工作:

$RevertTypeForJson.resources | ForEach-Object {
    $_.name = $_.name -replace "$(VariableForAzuredatafactory)", "$(VariableForAzuredatafactory)_NewValue"
    $_.properties.displayName = $_.properties.displayName -replace "$(VariableForAzuredatafactory)", "$(VariableForAzuredatafactory)_NewValue"
}
© www.soinside.com 2019 - 2024. All rights reserved.