json - 使用 PowerShell for Packer 编辑 JSON

标签 json powershell formatting packer

enter image description here

我需要一些帮助或建议。

我正在尝试构建一个 PowerShell 脚本来使用一些 JSON 值更新模板打包程序文件,但我失败得很厉害。

希望有人能提供建议吗?

中间部分是我试图在其中插入对象的模板 JSON。它应该在“builders”数组中(?),但最终被添加到“builders”之后...

如果需要,我会发布

最初位于 template.json 中的构建器:

"builders": [
    {
        "type": "vsphere-iso",
        "vcenter_server": "{{user `vsphere-server`}}",
        "insecure_connection": "true",
        "username": "{{user `vsphere-user`}}",
        "password": "{{user `vsphere-password`}}",
        "cluster": "{{user `vsphere-cluster`}}",
        "datacenter": "{{user `vsphere-datacenter`}}",
        "folder": "{{user `vsphere-folder`}}",
        "communicator": "winrm",
        "winrm_username": "Administrator",
        "winrm_password": "{{user `winadmin-password`}}",
        "convert_to_template": "true",
        "CPUs": "{{user `vm-cpu-num`}}",
        "RAM": "{{user `vm-mem-size`}}",
        "network": "{{user `vm-network`}}",
        "network_card": "vmxnet3",
        "datastore": "{{user `vsphere-datastore`}}",
        "disk_controller_type": "pvscsi",
        "guest_os_type": "windows9Server64Guest",
        "disk_thin_provisioned": true,
        "disk_size": "{{user `vm-disk-size`}}",
        "floppy_img_path": "[BN001-Local-DataStore01] contentlib-a036a42c-4504-44db-9d1a-3e9550954bf7/2b074347-88e4-408e-b228-d890308fa96d/pvscsi_88204189-95a8-4dbc-8fdb-8b4249c07c6f.flp",
        "floppy_files": [
            "../tmp/"
        ]
    }
]

编辑此内容的 PS 代码:

    $JSON = Get-Content -Path "$CurrentPath\template.json" -Raw | ConvertFrom-Json

$jsonContent = [PSCustomObject]@{
    'vm_name'   = $buildName
    'iso_paths' = @($vm_tools_iso , $os_iso) 
}

$JSON.builders += $jsonContent
$JSON | ConvertTo-Json -Depth 4 | Out-File $buildFile -Encoding Ascii -Force

预期结果

  "builders": [
{
  "type": "vsphere-iso",
  "vcenter_server": "{{user `vsphere-server`}}",
  "insecure_connection": "true",
  "username": "{{user `vsphere-user`}}",
  "password": "{{user `vsphere-password`}}",
  "cluster": "{{user `vsphere-cluster`}}",
  "datacenter": "{{user `vsphere-datacenter`}}",
  "folder": "{{user `vsphere-folder`}}",
  "communicator": "winrm",
  "winrm_username": "Administrator",
  "winrm_password": "{{user `winadmin-password`}}",
  "convert_to_template": "true",
  "CPUs": "{{user `vm-cpu-num`}}",
  "RAM": "{{user `vm-mem-size`}}",
  "network": "{{user `vm-network`}}",
  "network_card": "vmxnet3",
  "datastore": "{{user `vsphere-datastore`}}",
  "disk_controller_type": "pvscsi",
  "guest_os_type": "windows9Server64Guest",
  "disk_thin_provisioned": true,
  "disk_size": "{{user `vm-disk-size`}}",
  "floppy_img_path": "[BN001-Local-DataStore01] contentlib-a036a42c-4504-44db-9d1a-3e9550954bf7/2b074347-88e4-408e-b228-d890308fa96d/pvscsi_88204189-95a8-4dbc-8fdb-8b4249c07c6f.flp",
  "floppy_files": [
    "../tmp/"
  ],
  "vm_name": "server_2019_core",
  "iso_paths": [
    "[BN001-Local-DataStore01] contentlib-a036a42c-4504-44db-9d1a-3e9550954bf7/5bd0d88d-9817-4c54-ae04-95be5e9212b9/VMware-Tools-Windows-11_0_1-14773994_a816dc3a-6df1-42ff-9332-e7fcd8829d88.iso",
    "[BN001-Local-DataStore01] contentlib-a036a42c-4504-44db-9d1a-3e9550954bf7/2baac3c5-97b0-439c-aece-3b053e82b149/SW_DVD9_Win_Server_STD_CORE_2019_1809.11_64Bit_English_DC_STD_MLF_X22-51041_6afb1d25-5be2-4774-b071-77c349beb238.ISO"
  ]
}
]

实际结果

"builders": [
{
  "type": "vsphere-iso",
  "vcenter_server": "{{user `vsphere-server`}}",
  "insecure_connection": "true",
  "username": "{{user `vsphere-user`}}",
  "password": "{{user `vsphere-password`}}",
  "cluster": "{{user `vsphere-cluster`}}",
  "datacenter": "{{user `vsphere-datacenter`}}",
  "folder": "{{user `vsphere-folder`}}",
  "communicator": "winrm",
  "winrm_username": "Administrator",
  "winrm_password": "{{user `winadmin-password`}}",
  "convert_to_template": "true",
  "CPUs": "{{user `vm-cpu-num`}}",
  "RAM": "{{user `vm-mem-size`}}",
  "network": "{{user `vm-network`}}",
  "network_card": "vmxnet3",
  "datastore": "{{user `vsphere-datastore`}}",
  "disk_controller_type": "pvscsi",
  "guest_os_type": "windows9Server64Guest",
  "disk_thin_provisioned": true,
  "disk_size": "{{user `vm-disk-size`}}",
  "floppy_img_path": "[BN001-Local-DataStore01] contentlib-a036a42c-4504-44db-9d1a-3e9550954bf7/2b074347-88e4-408e-b228-d890308fa96d/pvscsi_88204189-95a8-4dbc-8fdb-8b4249c07c6f.flp",
  "floppy_files": [
    "../tmp/"
  ]
},
{
  "vm_name": "server_2019_core",
  "iso_paths": [
    "[BN001-Local-DataStore01] contentlib-a036a42c-4504-44db-9d1a-3e9550954bf7/5bd0d88d-9817-4c54-ae04-95be5e9212b9/VMware-Tools-Windows-11_0_1-14773994_a816dc3a-6df1-42ff-9332-e7fcd8829d88.iso",
    "[BN001-Local-DataStore01] contentlib-a036a42c-4504-44db-9d1a-3e9550954bf7/2baac3c5-97b0-439c-aece-3b053e82b149/SW_DVD9_Win_Server_STD_CORE_2019_1809.11_64Bit_English_DC_STD_MLF_X22-51041_6afb1d25-5be2-4774-b071-77c349beb238.ISO"
  ]
}
]

最佳答案

PowerShell 版本 6.0 开始,使用 ConvertFrom-Json 使任务变得稍微容易一些开关 -AsHashTable 。向下滚动查看 PS 5 解决方案。

这会输出 hashtable而不是默认的 PSCustomObject 。哈希表的优点是可以直接修改它,仅使用基本的 PowerShell 语法。

# Read the JSON file into a hashtable variable
$JSON = Get-Content -Path "$CurrentPath\template.json" -Raw | ConvertFrom-Json -AsHashTable

# Assign new properties to the first element of array <builders>
$JSON.builders[0].vm_name   = $buildName
$JSON.builders[0].iso_paths = $vm_tools_iso, $os_iso

# Write the JSON to a file
$JSON | ConvertTo-Json -Depth 4 | Out-File $buildFile -Encoding Ascii -Force

ConvertTo-Json知道如何处理哈希表,因此不需要特殊的开关。

请注意,新属性可能出现在序列化 JSON 对象中的任何位置,因为哈希表没有定义的顺序。但这不是问题,因为 JSON 对象中的属性顺序不相关。


PS 5 的替代解决方案,使用 Add-Member :

# Read the JSON file into a PSCustomObject
$JSON = Get-Content -Path "$CurrentPath\template.json" -Raw | ConvertFrom-Json

# Assign new properties to the first element of array <builders>
$JSON.builders[0] | Add-Member @{ 
    vm_name = $buildName
    iso_paths = $vm_tools_iso, $os_iso 
}

# Write the JSON to a file
$JSON | ConvertTo-Json -Depth 4 | Out-File $buildFile -Encoding Ascii -Force

刚刚了解 Add-Member <Hashtable> 后语法,它是 Add-Member -NotePropertyMembers <Hashtable> 的缩写形式,我认为 PS 5 变体与 PS 6+ 解决方案相比并没有那么糟糕。

关于json - 使用 PowerShell for Packer 编辑 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66053612/

相关文章:

java - Lotus Notes Java 代理的 GSON 库错误 - java.lang.NoClassDefFoundError : com. google.gson.JsonObject

powershell - 如何修复 Windows 10 中 powershell 中的下载文件错误

vba - excel vba 插入带格式的行

java - 图片与编号列表对齐 (Android)

c# - {System.Net.Sockets.SocketException(10054): An existing connection was forcibly closed by the remote host.} C#

formatting - 抑制 Fortran 95 写入语句中的换行符

ios - 无法使用 restkit 映射嵌套数组 json

javascript - 配置文件: JS vs INI

javascript - jQuery .html() 大负载

powershell - 对函数参数中声明的不存在文件进行强类型检查