简体   繁体   中英

Editing JSON with PowerShell for Packer

在此处输入图像描述

I would need some help or advise please.

I am trying to build a PowerShell script to update a template packer file with some JSON values, but I am failing horribly.

Hopefully someone can advise?

The middle portion is the template JSON into which I am trying to insert an object. it should be within the "builders" array(?), but it ends up being added after the "builders"...

If needed, I will post the

Builders originally within the 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 code to edit this:

    $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

Expected outcome

  "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"
  ]
}
]

Actual outcome

"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"
  ]
}
]

Beginning with PowerShell version 6.0 , the task becomes slightly easier by using the ConvertFrom-Json switch -AsHashTable . Scroll down for a PS 5 solution.

This outputs a hashtable instead of the default PSCustomObject . An advantage of a hashtable is that it can be directly modified, using only basic PowerShell syntax.

# 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 knows how to handle a hashtable, so there is no special switch needed.

Note that the new properties may appear in any position within the serialized JSON object, because a hashtable has no defined order. This is not a problem though, because the order of properties within a JSON object isn't relevant.


Alternative solution for PS 5 , using 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

After just learning about the Add-Member <Hashtable> syntax, which is short form of Add-Member -NotePropertyMembers <Hashtable> , I think the PS 5 variant isn't that bad compared to the PS 6+ solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM