Powershell: How to use Format-Table with XML data

流过昼夜 提交于 2019-12-30 04:48:07

问题


<tickets type="array">
    <ticket>
        <assigned-user-id type="integer">123</assigned-user-id>
        <closed type="boolean">true</closed>
        <creator-id type="integer">177522</creator-id>
        <number type="integer">306</number>
        <state>resolved</state>
        <tag nil="true"/>
        <title>
        title text 1
        </title>
        <updated-at type="datetime">2012-03-14T13:13:11+11:00</updated-at>
        <user-id type="integer">96438</user-id>
        <version type="integer">3</version>
        <user-name>Username</user-name>
    </ticket>
</tickets>

I am a Powershell newbie and find a question on xml and format-table. Given above xml file. If I run below script to display tickets in a table, the value of "number", "closed" could not be shown

$t = [xml](new-object system.net.webclient).downloadstring($xmlfilepath)
$t.tickets.ticket | Format-Table -Property title, state, user-name, url, number, closed

Return:

title            state       user-name       number       closed                                      
-----            -----       ---------       ------       ------                                
title text 1     resolved    Username        number       closed   
title text 2     resolved    Username        number       closed   

Is it the only way I have to use foreach and selectSingleNode("ticket").get_InnerXml() to get all the values?

Thank you.


回答1:


If you notice those nodes have attributes so you will need to get to the data of the node. try the following:

$t.tickets.ticket | Format-Table -AutoSize -Property title, state, user-name, url,
@{Label="number"; Expression={$_.number."#text"}},
@{Label="closed"; Expression={$_.closed."#text"}}


来源:https://stackoverflow.com/questions/9797693/powershell-how-to-use-format-table-with-xml-data

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