将 JSON 转换为 PowerShell 对象,然后用新对象替换该对象并转换回 JSON
Convert JSON to a PowerShell Object, then, replace that Object with a new Object and convert back to JSON
背景
我有一些 JSON 用于 Packer 构建,如下所示:
{
"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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{user `installer_script_folder`}}",
"DOCKERHUB_LOGIN\u003d{{user `dockerhub_login`}}",
"DOCKERHUB_PASSWORD\u003d{{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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{user `installer_script_folder`}}",
"DEBIAN_FRONTEND\u003dnoninteractive"
],
"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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{user `installer_script_folder`}}",
"DOCKERHUB_LOGIN\u003d{{user `dockerhub_login`}}",
"DOCKERHUB_PASSWORD\u003d{{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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{user `installer_script_folder`}}",
"DEBIAN_FRONTEND\u003dnoninteractive"
],
"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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{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 object 的一部分转换回 JSON string。使用 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
背景
我有一些 JSON 用于 Packer 构建,如下所示:
{
"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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{user `installer_script_folder`}}",
"DOCKERHUB_LOGIN\u003d{{user `dockerhub_login`}}",
"DOCKERHUB_PASSWORD\u003d{{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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{user `installer_script_folder`}}",
"DEBIAN_FRONTEND\u003dnoninteractive"
],
"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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{user `installer_script_folder`}}",
"DOCKERHUB_LOGIN\u003d{{user `dockerhub_login`}}",
"DOCKERHUB_PASSWORD\u003d{{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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{user `installer_script_folder`}}",
"DEBIAN_FRONTEND\u003dnoninteractive"
],
"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_SCRIPTS\u003d{{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER\u003d{{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 object 的一部分转换回 JSON string。使用 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