PowerShell: Custom Property XML Tags with ConvertTo-XML Output

允我心安 提交于 2019-12-08 16:13:50

问题


I am creating a new object in PowerShell, using a hash table to set property values. I want to then export the object into XML format using the ConvertTo-XML method.

$hash = @{            
    Processor = 'Intel'                
    Disk = '500GB'
    Server = 'ABC'
    Serial = '01234'
}                           

$specs = New-Object PSObject -Property $hash
Write-Output ($specs | ConvertTo-XML -notypeinformation).Save("C:\scripts\export.xml")

The XML output is as follows:

<Objects>
  <Object>
    <Property Name="Serial">a1b2c3</Property>
    <Property Name="Server">ABC</Property>
    <Property Name="Processor">Intel</Property>
    <Property Name="Disk">500GB</Property>
  </Object>
</Objects>

What I want, is for the XML tags to be formatted in the following way:

<Objects>
  <Object>
    <Serial>a1b2c3</Serial>
    <Server>ABC</Server>
    <Processor>Intel</Processor>
    <Disk>500GB</Disk>
  </Object>
</Objects>

And then, if there is a good solution for that, is there also a way to make the Object(s) tags custom as well?

Thank you.


回答1:


I don't think you can get there with ConvertTo-Xml. However, you can use here strings to do this. It is kind of low tech but still pretty cool:

$hash = @{            
    Processor = 'Intel'                
    Disk = '500GB'
    Server = 'ABC'
    Serial = '01234'
}                           

@"
<Objects>
  <Object>$(foreach ($kvpair in $hash.GetEnumerator()) {
    "`n    <$($kvpair.Key)>$($kvpair.Value)</$($kvpair.Key)>"
})
  </Object>
</Objects>
"@ > C:\scripts\export.xml

You could use the XML DOM to create this document but that would be more work and for such a simple document I think the here string approach works pretty well. It is also good for any sort of text templating.




回答2:


Here's a one-liner:

$specs |% {'<Objects>'} {$_.psobject.properties |% {'  <Object>'} {"    <$($_.name)>$($_.value)<\$($_.name)>"} {'  <\Object>'} } {'<\Objects>'}



回答3:


Mjolinor's one liner is very useful for outputting from Powershell custom object tables in the XML format described above. However there is an error in it - the closing "/" xml tag is the wrong way round. This means that the XML code is invalid and give a '"XML Parsing Error: not well-formed"' error. Took me a while to figure out what was wrong, here is the corrected code so others do not have to figure it out again:

    $SelectedResults |% {'<Objects>'} {$_.psobject.properties |% {'  <Object>'} {"    <$($_.name)>$($_.value)</$($_.name)>"} {'  </Object>'} } {'</Objects>'}

Where $SelectedResults is the output of the PS Custom object.

You can test XML output and if it is valid here - https://www.w3schools.com/xml/xml_validator.asp




回答4:


The selected answer worked for me but I had to change this line

"`n    <$($kvpair.Key)>$($kvpair.Value)</$($kvpair.Key)>"

to

"`n    <$($kvpair.Keys)>$($kvpair.Values)</$($kvpair.Keys)>"

Keys and Values instead of Key and Value

Also, if you want the spaces to be visible in notepad, use

`r`n

(I don't have enough reputation to comment on the answer)



来源:https://stackoverflow.com/questions/19935102/powershell-custom-property-xml-tags-with-convertto-xml-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!