Powershell:如何将 Format-Table 与 XML 数据一起使用 [英] Powershell: How to use Format-Table with XML data

查看:35
本文介绍了Powershell:如何将 Format-Table 与 XML 数据一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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 和格式表的问题.上面给出了 xml 文件.如果我运行下面的脚本以在表格中显示票证,则无法显示数字"、关闭"的值

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

返回:

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

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

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

谢谢.

推荐答案

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

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

这篇关于Powershell:如何将 Format-Table 与 XML 数据一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆