将JSON转换为PowerShell对象,然后将该对象替换为新对象并转换回JSON



背景

我有一些用于Packer构建的JSON,如下所示:

{
"builders": [
{
"type": "azure-arm"
}
],
"provisioners": [
{
"type": "shell",
"scripts": [
"{{template_dir}}/scripts/installers/docker-compose.sh",
"{{template_dir}}/scripts/installers/docker-moby.sh"
],
"environment_vars": [
"HELPER_SCRIPTSu003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDERu003d{{user `installer_script_folder`}}",
"DOCKERHUB_LOGINu003d{{user `dockerhub_login`}}",
"DOCKERHUB_PASSWORDu003d{{user `dockerhub_password`}}"
],
"execute_command": "sudo sh -c u0027{{ .Vars }} {{ .Path }}u0027"
},
{
"type": "shell",
"scripts": [
"{{template_dir}}/scripts/installers/azcopy.sh",
"{{template_dir}}/scripts/installers/azure-cli.sh",
"{{template_dir}}/scripts/installers/azure-devops-cli.sh",
"{{template_dir}}/scripts/installers/basic.sh",
"{{template_dir}}/scripts/installers/bicep.sh",
"{{template_dir}}/scripts/installers/aliyun-cli.sh",
"{{template_dir}}/scripts/installers/apache.sh",
"{{template_dir}}/scripts/installers/aws.sh",
"{{template_dir}}/scripts/installers/clang.sh",
"{{template_dir}}/scripts/installers/swift.sh",
"{{template_dir}}/scripts/installers/cmake.sh",
"{{template_dir}}/scripts/installers/codeql-bundle.sh",
"{{template_dir}}/scripts/installers/containers.sh",
"{{template_dir}}/scripts/installers/dotnetcore-sdk.sh",
"{{template_dir}}/scripts/installers/erlang.sh",
"{{template_dir}}/scripts/installers/firefox.sh",
"{{template_dir}}/scripts/installers/gcc.sh",
"{{template_dir}}/scripts/installers/gfortran.sh",
"{{template_dir}}/scripts/installers/git.sh",
"{{template_dir}}/scripts/installers/github-cli.sh",
"{{template_dir}}/scripts/installers/google-chrome.sh",
"{{template_dir}}/scripts/installers/google-cloud-sdk.sh",
"{{template_dir}}/scripts/installers/haskell.sh",
"{{template_dir}}/scripts/installers/heroku.sh",
"{{template_dir}}/scripts/installers/hhvm.sh",
"{{template_dir}}/scripts/installers/java-tools.sh",
"{{template_dir}}/scripts/installers/kubernetes-tools.sh",
"{{template_dir}}/scripts/installers/oc.sh",
"{{template_dir}}/scripts/installers/leiningen.sh",
"{{template_dir}}/scripts/installers/miniconda.sh",
"{{template_dir}}/scripts/installers/mono.sh",
"{{template_dir}}/scripts/installers/kotlin.sh",
"{{template_dir}}/scripts/installers/mysql.sh",
"{{template_dir}}/scripts/installers/mssql-cmd-tools.sh",
"{{template_dir}}/scripts/installers/sqlpackage.sh",
"{{template_dir}}/scripts/installers/nginx.sh",
"{{template_dir}}/scripts/installers/nvm.sh",
"{{template_dir}}/scripts/installers/nodejs.sh",
"{{template_dir}}/scripts/installers/bazel.sh",
"{{template_dir}}/scripts/installers/oras-cli.sh",
"{{template_dir}}/scripts/installers/phantomjs.sh",
"{{template_dir}}/scripts/installers/php.sh",
"{{template_dir}}/scripts/installers/postgresql.sh",
"{{template_dir}}/scripts/installers/pulumi.sh",
"{{template_dir}}/scripts/installers/ruby.sh",
"{{template_dir}}/scripts/installers/r.sh",
"{{template_dir}}/scripts/installers/rust.sh",
"{{template_dir}}/scripts/installers/julia.sh",
"{{template_dir}}/scripts/installers/sbt.sh",
"{{template_dir}}/scripts/installers/selenium.sh",
"{{template_dir}}/scripts/installers/smallstep-cli.sh",
"{{template_dir}}/scripts/installers/terraform.sh",
"{{template_dir}}/scripts/installers/packer.sh",
"{{template_dir}}/scripts/installers/vcpkg.sh",
"{{template_dir}}/scripts/installers/dpkg-config.sh",
"{{template_dir}}/scripts/installers/mongodb.sh",
"{{template_dir}}/scripts/installers/android.sh",
"{{template_dir}}/scripts/installers/yq.sh",
"{{template_dir}}/scripts/installers/pypy.sh",
"{{template_dir}}/scripts/installers/python.sh",
"{{template_dir}}/scripts/installers/graalvm.sh"
],
"environment_vars": [
"HELPER_SCRIPTSu003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDERu003d{{user `installer_script_folder`}}",
"DEBIAN_FRONTENDu003dnoninteractive"
],
"execute_command": "sudo sh -c u0027{{ .Vars }} {{ .Path }}u0027"
},
{
"type": "shell",
"scripts": [
"{{template_dir}}/scripts/installers/Install-Toolset.ps1",
"{{template_dir}}/scripts/installers/Configure-Toolset.ps1"
],
"environment_vars": [
"HELPER_SCRIPTSu003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDERu003d{{user `installer_script_folder`}}"
],
"execute_command": "sudo sh -c u0027{{ .Vars }} pwsh -f {{ .Path }}u0027"
}
]
}

我试图在provisioners列表中添加一些项,但仅在名为scripts的第二个索引列表中添加,因此在本例中为scripts对象。理想情况下,这一切都在PowerShell中完成(我使用的是PowerShell 7最新版本)。

我尝试过的

通过谷歌搜索,有人建议我将JSON转换为PowerShell对象,然后尝试添加到其中:

$JsonFile = "build.json"
$JsonObject = $(Get-Content $JsonFile -Raw)
$SystemObject=$($JsonObject | ConvertFrom-Json)
$OldScriptsJson = $SystemObject.provisioners[1].scripts | ConvertTo-Json

这给了我想要添加到的旧JSON:

[
"{{template_dir}}/scripts/installers/azcopy.sh",
"{{template_dir}}/scripts/installers/azure-cli.sh",
"{{template_dir}}/scripts/installers/azure-devops-cli.sh",
"{{template_dir}}/scripts/installers/basic.sh",
"{{template_dir}}/scripts/installers/bicep.sh",
"{{template_dir}}/scripts/installers/aliyun-cli.sh",
"{{template_dir}}/scripts/installers/apache.sh",
"{{template_dir}}/scripts/installers/aws.sh",
"{{template_dir}}/scripts/installers/clang.sh",
"{{template_dir}}/scripts/installers/swift.sh",
"{{template_dir}}/scripts/installers/cmake.sh",
"{{template_dir}}/scripts/installers/codeql-bundle.sh",
"{{template_dir}}/scripts/installers/containers.sh",
"{{template_dir}}/scripts/installers/dotnetcore-sdk.sh",
"{{template_dir}}/scripts/installers/erlang.sh",
"{{template_dir}}/scripts/installers/firefox.sh",
"{{template_dir}}/scripts/installers/gcc.sh",
"{{template_dir}}/scripts/installers/gfortran.sh",
"{{template_dir}}/scripts/installers/git.sh",
"{{template_dir}}/scripts/installers/github-cli.sh",
"{{template_dir}}/scripts/installers/google-chrome.sh",
"{{template_dir}}/scripts/installers/google-cloud-sdk.sh",
"{{template_dir}}/scripts/installers/haskell.sh",
"{{template_dir}}/scripts/installers/heroku.sh",
"{{template_dir}}/scripts/installers/hhvm.sh",
"{{template_dir}}/scripts/installers/java-tools.sh",
"{{template_dir}}/scripts/installers/kubernetes-tools.sh",
"{{template_dir}}/scripts/installers/oc.sh",
"{{template_dir}}/scripts/installers/leiningen.sh",
"{{template_dir}}/scripts/installers/miniconda.sh",
"{{template_dir}}/scripts/installers/mono.sh",
"{{template_dir}}/scripts/installers/kotlin.sh",
"{{template_dir}}/scripts/installers/mysql.sh",
"{{template_dir}}/scripts/installers/mssql-cmd-tools.sh",
"{{template_dir}}/scripts/installers/sqlpackage.sh",
"{{template_dir}}/scripts/installers/nginx.sh",
"{{template_dir}}/scripts/installers/nvm.sh",
"{{template_dir}}/scripts/installers/nodejs.sh",
"{{template_dir}}/scripts/installers/bazel.sh",
"{{template_dir}}/scripts/installers/oras-cli.sh",
"{{template_dir}}/scripts/installers/phantomjs.sh",
"{{template_dir}}/scripts/installers/php.sh",
"{{template_dir}}/scripts/installers/postgresql.sh",
"{{template_dir}}/scripts/installers/pulumi.sh",
"{{template_dir}}/scripts/installers/ruby.sh",
"{{template_dir}}/scripts/installers/r.sh",
"{{template_dir}}/scripts/installers/rust.sh",
"{{template_dir}}/scripts/installers/julia.sh",
"{{template_dir}}/scripts/installers/sbt.sh",
"{{template_dir}}/scripts/installers/selenium.sh",
"{{template_dir}}/scripts/installers/smallstep-cli.sh",
"{{template_dir}}/scripts/installers/terraform.sh",
"{{template_dir}}/scripts/installers/packer.sh",
"{{template_dir}}/scripts/installers/vcpkg.sh",
"{{template_dir}}/scripts/installers/dpkg-config.sh",
"{{template_dir}}/scripts/installers/mongodb.sh",
"{{template_dir}}/scripts/installers/android.sh",
"{{template_dir}}/scripts/installers/yq.sh",
"{{template_dir}}/scripts/installers/pypy.sh",
"{{template_dir}}/scripts/installers/python.sh",
"{{template_dir}}/scripts/installers/graalvm.sh"
]

我想做什么

我基本上想在旧的JSON 中添加以下JSON

[
"{{template_dir}}/scripts/installers/colordiff.sh",
"{{template_dir}}/scripts/installers/kubelogin.sh",
"{{template_dir}}/scripts/installers/kubeval.sh",
"{{template_dir}}/scripts/installers/kubeaudit.sh",
"{{template_dir}}/scripts/installers/smallstep-cli.sh"
]

因此,所有这些的最终产品都可以保存回原始的JSON:

{
"builders": [
{
"type": "azure-arm"
}
],
"provisioners": [
{
"type": "shell",
"scripts": [
"{{template_dir}}/scripts/installers/docker-compose.sh",
"{{template_dir}}/scripts/installers/docker-moby.sh"
],
"environment_vars": [
"HELPER_SCRIPTSu003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDERu003d{{user `installer_script_folder`}}",
"DOCKERHUB_LOGINu003d{{user `dockerhub_login`}}",
"DOCKERHUB_PASSWORDu003d{{user `dockerhub_password`}}"
],
"execute_command": "sudo sh -c u0027{{ .Vars }} {{ .Path }}u0027"
},
{
"type": "shell",
"scripts": [
"{{template_dir}}/scripts/installers/azcopy.sh",
"{{template_dir}}/scripts/installers/azure-cli.sh",
"{{template_dir}}/scripts/installers/azure-devops-cli.sh",
"{{template_dir}}/scripts/installers/basic.sh",
"{{template_dir}}/scripts/installers/bicep.sh",
"{{template_dir}}/scripts/installers/aliyun-cli.sh",
"{{template_dir}}/scripts/installers/apache.sh",
"{{template_dir}}/scripts/installers/aws.sh",
"{{template_dir}}/scripts/installers/clang.sh",
"{{template_dir}}/scripts/installers/swift.sh",
"{{template_dir}}/scripts/installers/cmake.sh",
"{{template_dir}}/scripts/installers/codeql-bundle.sh",
"{{template_dir}}/scripts/installers/containers.sh",
"{{template_dir}}/scripts/installers/dotnetcore-sdk.sh",
"{{template_dir}}/scripts/installers/erlang.sh",
"{{template_dir}}/scripts/installers/firefox.sh",
"{{template_dir}}/scripts/installers/gcc.sh",
"{{template_dir}}/scripts/installers/gfortran.sh",
"{{template_dir}}/scripts/installers/git.sh",
"{{template_dir}}/scripts/installers/github-cli.sh",
"{{template_dir}}/scripts/installers/google-chrome.sh",
"{{template_dir}}/scripts/installers/google-cloud-sdk.sh",
"{{template_dir}}/scripts/installers/haskell.sh",
"{{template_dir}}/scripts/installers/heroku.sh",
"{{template_dir}}/scripts/installers/hhvm.sh",
"{{template_dir}}/scripts/installers/java-tools.sh",
"{{template_dir}}/scripts/installers/kubernetes-tools.sh",
"{{template_dir}}/scripts/installers/oc.sh",
"{{template_dir}}/scripts/installers/leiningen.sh",
"{{template_dir}}/scripts/installers/miniconda.sh",
"{{template_dir}}/scripts/installers/mono.sh",
"{{template_dir}}/scripts/installers/kotlin.sh",
"{{template_dir}}/scripts/installers/mysql.sh",
"{{template_dir}}/scripts/installers/mssql-cmd-tools.sh",
"{{template_dir}}/scripts/installers/sqlpackage.sh",
"{{template_dir}}/scripts/installers/nginx.sh",
"{{template_dir}}/scripts/installers/nvm.sh",
"{{template_dir}}/scripts/installers/nodejs.sh",
"{{template_dir}}/scripts/installers/bazel.sh",
"{{template_dir}}/scripts/installers/oras-cli.sh",
"{{template_dir}}/scripts/installers/phantomjs.sh",
"{{template_dir}}/scripts/installers/php.sh",
"{{template_dir}}/scripts/installers/postgresql.sh",
"{{template_dir}}/scripts/installers/pulumi.sh",
"{{template_dir}}/scripts/installers/ruby.sh",
"{{template_dir}}/scripts/installers/r.sh",
"{{template_dir}}/scripts/installers/rust.sh",
"{{template_dir}}/scripts/installers/julia.sh",
"{{template_dir}}/scripts/installers/sbt.sh",
"{{template_dir}}/scripts/installers/selenium.sh",
"{{template_dir}}/scripts/installers/smallstep-cli.sh",
"{{template_dir}}/scripts/installers/terraform.sh",
"{{template_dir}}/scripts/installers/packer.sh",
"{{template_dir}}/scripts/installers/vcpkg.sh",
"{{template_dir}}/scripts/installers/dpkg-config.sh",
"{{template_dir}}/scripts/installers/mongodb.sh",
"{{template_dir}}/scripts/installers/android.sh",
"{{template_dir}}/scripts/installers/yq.sh",
"{{template_dir}}/scripts/installers/pypy.sh",
"{{template_dir}}/scripts/installers/python.sh",
"{{template_dir}}/scripts/installers/graalvm.sh",
"{{template_dir}}/scripts/installers/colordiff.sh",
"{{template_dir}}/scripts/installers/kubelogin.sh",
"{{template_dir}}/scripts/installers/kubeval.sh",
"{{template_dir}}/scripts/installers/kubeaudit.sh",
"{{template_dir}}/scripts/installers/smallstep-cli.sh"
],
"environment_vars": [
"HELPER_SCRIPTSu003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDERu003d{{user `installer_script_folder`}}",
"DEBIAN_FRONTENDu003dnoninteractive"
],
"execute_command": "sudo sh -c u0027{{ .Vars }} {{ .Path }}u0027"
},
{
"type": "shell",
"scripts": [
"{{template_dir}}/scripts/installers/Install-Toolset.ps1",
"{{template_dir}}/scripts/installers/Configure-Toolset.ps1"
],
"environment_vars": [
"HELPER_SCRIPTSu003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDERu003d{{user `installer_script_folder`}}"
],
"execute_command": "sudo sh -c u0027{{ .Vars }} pwsh -f {{ .Path }}u0027"
}
]
}

我如何使用PowerShell来完成这项工作?

这应该做到:

$json = Get-Content 'Build.json' -Raw | ConvertFrom-Json
# Define an array of strings to add.
$scriptsToAdd = @(
'{{template_dir}}/scripts/installers/colordiff.sh'
'{{template_dir}}/scripts/installers/kubelogin.sh'
'{{template_dir}}/scripts/installers/kubeval.sh'
'{{template_dir}}/scripts/installers/kubeaudit.sh'
'{{template_dir}}/scripts/installers/smallstep-cli.sh'
)
# Alternatively read these from a JSON file as well:
# $scriptsToAdd = Get-Content 'ScriptsToAdd.json' -Raw | ConvertFrom-Json
# Append the $scriptsToAdd array to the scripts array of the JSON
$json.provisioners[1].scripts += $scriptsToAdd
# Make sure to specify a large enough argument for -Depth, which defaults to 2.
# Otherwise objects that are nested deeper will be truncated.
$json | ConvertTo-Json -Depth 100 | Set-Content 'BuildNew.json'

至于你尝试过的:

$OldScriptsJson = $SystemObject.provisioners[1].scripts | ConvertTo-Json

您刚刚将原始JSON对象的一部分转换回JSON字符串。使用JSON时,应始终对对象进行操作,并且仅作为最后一步,将其转换回序列化的JSON,该JSON可以是字符串或文件。

$SystemObject = get-content build.json | convertfrom-json
# load changes into another object
$addobject = get-content add.json | convertfrom-json
# add 2 arrays together (+= kills puppies in bulk)
$SystemObject.provisioners[1].scripts += $addobject
# save, watch out for default depth of 2, the latest ps 7 gives a warning 
# "WARNING: Resulting JSON is truncated as serialization has exceeded the set depth
# of 2."
$SystemObject | ConvertTo-Json -Depth 3 | set-content build2.json

最新更新