Powershell:如何将Format-Table与XML数据一起使用

时间:2012-03-21 02:05:04

标签: xml powershell powershell-v2.0

<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>

我是Powershell的新手,在xml和format-table上找到了一个问题。给出上面的xml文件。如果我在脚本下面运行以在表格中显示票证,则&#34;数字&#34;,&#34;关闭&#34;无法显示

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

返回:

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

这是我使用foreachselectSingleNode("ticket").get_InnerXml()获取所有值的唯一方法吗?

谢谢。

1 个答案:

答案 0 :(得分:6)

如果您注意到这些节点具有属性,那么您将需要获取节点的数据。尝试以下方法:

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