Azure 管道 - 在 json 数组 VSTS 管道中添加新元素 (Appsettings.json) [英] Azure pipelines -Add new element in json array VSTS pipelines (Appsettings.json)

查看:42
本文介绍了Azure 管道 - 在 json 数组 VSTS 管道中添加新元素 (Appsettings.json)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Azure Release Pipeline 的 appsetting.json 数组中添加新元素?

Is this possible to add a new element in an array of appsetting.json in Azure Release Pipeline?

appsetting.json 中,我有数组变量,我需要在通过 Azure Pipeline 部署期间填充另一个元素.

In appsetting.json I have array variable which I need to fill with another element during deployment through Azure Pipeline.

  "Array": [
                {
                    "Name": "AD1",
                    "IsDefault": "true",
                    "IdPEntityId": "URL1",
                    "Metadata": "XMLpath1"
                },
                {
                    "Name": "AD2",
                    "IsDefault": "false",
                    "IdPEntityId": "URL2",
                    "Metadata": "XMLPath2"
                }
]

在上面的 JSON 数组中,我需要添加另一个元素的最后位置 (array-Index:2).

Here in the above JSON array I need to add another one elemental last position (array-Index:2).

推荐答案

[CmdletBinding()]

param(
    [string] $AdName,
    [bool]   $AdIsDefault,
    [string] $AdIdPEntityId, 
    [string] $AdMetadata,
    [string] $AppSettingFilePath  
)
clear-Host

Write-Host 'Updating appsettings.json...' -ForegroundColor Yellow

function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
  $indent = 0;
  ($json -Split '\n' |
    % {
      if ($_ -match '[\}\]]') {
        # This line contains  ] or }, decrement the indentation level
        $indent--
      }
      $line = (' ' * $indent * 2) + $_.TrimStart().Replace(':  ', ': ')
      if ($_ -match '[\{\[]') {
        # This line contains [ or {, increment the indentation level
        $indent++
      }
      $line
  }) -Join "`n"
}

$JsonDataAdd=@"
{
                    "Name":"$AdName",
                    "IsDefault": "$AdIsDefault",
                    "IdPEntityId":"$AdIdPEntityId",
                    "Metadata": "$AdMetadata"
}
"@
Write-Host ' Active directory details :' -ForegroundColor Yellow

Write-Host `n  $JsonDataAdd -ForegroundColor Green

$jsonData = Get-Content "$AppSettingFilePath" | Out-String | ConvertFrom-Json -ErrorAction Stop


$jsonData.IdentitySettings.ExternalProviders.Saml2Providers += (ConvertFrom-Json $JsonDataAdd)


$jsonData | ConvertTo-Json -Depth 10 |  Format-Json | Set-Content "$AppSettingFilePath" -Encoding UTF8

Write-Host 'Successfully Updated -appSettings.json  !' -ForegroundColor Yellow

这篇关于Azure 管道 - 在 json 数组 VSTS 管道中添加新元素 (Appsettings.json)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆