使用 powershell 脚本替换整个 json 对象

时间:2021-01-20 13:44:15

标签: powershell powershell-2.0

我正在尝试将一个 json 文件从一个环境部署到另一个环境。 但是我必须在部署时替换某些对象的属性。

为了实现这一点,我试图替换整个 json 对象,因为我必须替换大部分属性。 我正在使用 shell 脚本替换 json 对象,但无法这样做。

下面是源码中的json文件。

},
        {
            "name": "[concat(parameters('factoryName'), '/AzureDataLakeStore1')]",
            "type": "Microsoft.DataFactory/factories/linkedServices",
            "apiVersion": "2018-06-01",
            "properties": {
                "annotations": [],
                "type": "AzureDataLakeStore",
                "typeProperties": {
                    "dataLakeStoreUri": "[parameters('AzureDataLakeStore1_properties_typeProperties_dataLakeStoreUri')]",
                    "servicePrincipalId": "[parameters('AzureDataLakeStore1_properties_typeProperties_servicePrincipalId')]",
                    "servicePrincipalKey": {
                        "type": "AzureKeyVaultSecret",
                        "store": {
                            "referenceName": "eXtollokeyvault",
                            "type": "LinkedServiceReference"
                        },
                        "secretName": "[parameters('AzureDataLakeStore1_properties_typeProperties_servicePrincipalKey_secretName')]"
                    },
                    "tenant": "[parameters('AzureDataLakeStore1_properties_typeProperties_tenant')]",
                    "subscriptionId": "[parameters('AzureDataLakeStore1_properties_typeProperties_subscriptionId')]",
                    "resourceGroupName": "[parameters('AzureDataLakeStore1_properties_typeProperties_resourceGroupName')]"
                },
                "connectVia": {
                    "referenceName": "coe-eu-ir-azu-xbs",
                    "type": "IntegrationRuntimeReference"
                }
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/integrationRuntimes/coe-eu-ir-azu-xbs')]",
                "[concat(variables('factoryId'), '/linkedServices/eXtollokeyvault')]"
            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/AzureDataLakeStore2')]",
            "type": "Microsoft.DataFactory/factories/linkedServices",
            "apiVersion": "2018-06-01",
            "properties": {
                "annotations": [],
                "type": "AzureDataLakeStore",
                "typeProperties": {
                    "dataLakeStoreUri": "[parameters('AzureDataLakeStore2_properties_typeProperties_dataLakeStoreUri')]",
                    "servicePrincipalId": "[parameters('AzureDataLakeStore2_properties_typeProperties_servicePrincipalId')]",
                    "servicePrincipalKey": {
                        "type": "AzureKeyVaultSecret",
                        "store": {
                            "referenceName": "eXtollokeyvault",
                            "type": "LinkedServiceReference"
                        },
                        "secretName": "[parameters('AzureDataLakeStore2_properties_typeProperties_servicePrincipalKey_secretName')]"
                    },
                    "tenant": "[parameters('AzureDataLakeStore2_properties_typeProperties_tenant')]",
                    "subscriptionId": "[parameters('AzureDataLakeStore2_properties_typeProperties_subscriptionId')]",
                    "resourceGroupName": "[parameters('AzureDataLakeStore2_properties_typeProperties_resourceGroupName')]"
                },
                "connectVia": {
                    "referenceName": "coe-eu-ir-azu-xbs",
                    "type": "IntegrationRuntimeReference"
                }
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/integrationRuntimes/coe-eu-ir-azu-xbs')]",
                "[concat(variables('factoryId'), '/linkedServices/eXtollokeyvault')]"
            ]
        },

我必须用名称

更改整个对象
"name": "[concat(parameters('factoryName'), '/AzureDataLakeStore1')]",

带有 name 的整个对象 "name": "[concat(parameters('factoryName'), '/AzureDataLakeStore1')]", 应该如下所示。

},
        {
            "name": "[concat(parameters('factoryName'), '/AzureDataLakeStore1')]",
            "type": "Microsoft.DataFactory/factories/linkedServices",
            "apiVersion": "2018-06-01",
            "properties": {
                "annotations": [],
                "type": "AzureBlobFS",
                "typeProperties": {
                    "url": "[parameters('AzureDataLakeStore1_v2_properties_typeProperties_url')]"
                },
                "connectVia": {
                    "referenceName": "coe-eu-ir-azu-xbs",
                    "type": "IntegrationRuntimeReference"
                }
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/integrationRuntimes/coe-eu-ir-azu-xbs')]"
            ]
        },

必须像上面一样替换整个对象。

我已经尝试通过删除 object.properties 来使用 powershell 脚本,但无法按照我的要求创建对象。

$JsonData = Get-Content $json_path -raw | ConvertFrom-Json
$JsonData.resources | Where{$_.name -eq "[concat(parameters('factoryName'), '/AzureDataLakeStore1')]"} | ForEach{$_.PSObject.Properties.remove("properties")}
$JsonData.resources | Where{$_.name -eq "[concat(parameters('factoryName'), '/AzureDataLakeStore1')]"} | ForEach{$_.PSObject.Properties.remove("dependsOn")}

$JsonData | ConvertTo-Json -depth 50| % {[System.Text.RegularExpressions.Regex]::Unescape($_)} | set-content $json_path

有什么办法可以实现吗?

1 个答案:

答案 0 :(得分:0)

Azure Devops 是一种更改链接服务属性的简单方法,您可以为每个环境创建变量,然后在您的部署管道(发布管道)中提及这一点

这是一个demo

相关问题