Reputation: 31
Trying to import a values from Xml
<configuration>
<properties>
<subtype type="integer">1</subtype>
<name type="string">packer-centos8-base-g2-10</name>
</properties>
<settings>
<processors>
<count type="integer">4</count>
</processors>
<memory>
<bank>
<dynamic_memory_enabled type="bool">False</dynamic_memory_enabled>
<limit type="integer">1048576</limit>
<reservation type="integer">512</reservation>
<size type="integer">4096</size>
</bank>
</memory>
</settings>
<AltSwitchName type="string">Default Switch</AltSwitchName>
<boot>
<device0 type="string">Optical</device0>
</boot>
<secure_boot_enabled type="bool">False</secure_boot_enabled>
<notes type="string"> </notes>
<vm-controllers>
<scsi ChannelInstanceGuid="x">
<controller0>
<drive0>
<pathname type="string">
C:\San\SV65\hyper-packer\hyper-packer\hyperv314337150\packer-centos8-base-g2-10.vhdx
</pathname>
<type type="string">VHD</type>
</drive0>
</controller0>
</scsi>
</vm-controllers>
</configuration>
Output from the Powershell commands
PS C:\San\SV65\ps> $jvariables=Get-Content -Raw -Path '.\variables-ps.json' | ConvertFrom-Json
PS C:\San\SV65\ps> $VMXml = Get-ChildItem -path $jvariables.vmfp -recurse -include *.xml
PS C:\San\SV65\ps> write-host $jvariables.vmfp
C:\San\SV65\hyper-packer\hyper-packer\output-centos8-10\Virtual Machines\
PS C:\San\SV65\ps> write-host $VMXml
C:\San\SV65\hyper-packer\hyper-packer\output-centos8-10\Virtual Machines\box.xml
PS C:\San\SV65\ps> write-host $VMbox
PS C:\San\SV65\ps>
$VMbox gets a null value, I am trying to extract a value from the xml
$name = $VMbox.configuration.properties.name
is this because of the spaces in btn "Virtual Machines"? in $VMXml?
If its gets populated from variables.json
$jvariables=Get-Content -Raw -Path '.\variables-ps.json' | ConvertFrom-Json
{
"psfp": "C:\\San\\SV65\\ps\\",
"vmfp": "C:\\San\\SV65\\hyper-packer\\hyper-packer\\output-centos8-10\\Virtual Machines\\",
"psmp": "C:\\San\\SV65\\ps\\",
"vmname": ""
}
How to add the values in the variable as 'value' in this format?
Even if I add, it's not getting executed in the powershell.
In summary, I want to extract the path from json and try to get the value from Xml and use in the code.
Upvotes: 0
Views: 127
Reputation: 61028
You forgot to load the xml file after you get the path in variable $VMXml
.
Also, it is very likely/possible that by using
$VMXml = Get-ChildItem -path $jvariables.vmfp -recurse -include *.xml
this variable holds an array of FileInfo and/or DirectoryInfo objects, so better use something like
$VMXml = Get-ChildItem -Path $jvariables.vmfp -Recurse -Filter '*.xml' -File | Select-Object -First 1
to get just one FileInfo object.
Then instantiate the variable $VMbox
using
[xml]$VMbox = Get-Content -Path $VMXml.FullName
and get the values from that with your code $name = $VMbox.configuration.properties.name
Hope that helps
Upvotes: 1