【问题标题】:Sort and Display XML Output in Powershell在 Powershell 中排序和显示 XML 输出
【发布时间】:2020-09-29 04:40:34
【问题描述】:

我有一个名为 file.xml 的文件,格式如下:

<Sources>
  <add id="TestName" type="TestType" priority="1">
    <parameters>
      <otherIdentifiers>TestNameIdentifier</otherIdentifiers>
      <reportSource>TestReportSource</reportSource>
      <activeStatus>true</activeStatus>
      <UploadStatus>false</UploadStatus>
      <displayTypes>Image\,Report</displayTypes>
      <address.host>192.168.1.2</address.host>
    </parameters>
  </add>
  <add id="TestName2" type="TestType" priority="2">
    <parameters>
      <otherIdentifiers>TestName2Identifier</otherIdentifiers>
      <reportSource>TestReportSource2</reportSource>
      <activeStatus>true</activeStatus>
      <UploadStatus>false</UploadStatus>
      <displayTypes>Image\,Report</displayTypes>
      <address.host>192.168.1.3</address.host>
    </parameters>
  </add>
</Sources>

在 Powershell 中,我将我的文件声明为:

[xml]$File = gc file.xml

我感兴趣的具体值是

$File.Sources.add.id
$File.Sources.add.type
$File.Sources.add.priority
$File.Sources.add.parameter.activeStatus
$File.Sources.add.parameter."address.host"

我知道我可以按照上面的格式单独获取它们,但我正在努力弄清楚如何在表格中显示每个添加节点的信息,格式如下:

Source     Type     Priority     Active     IP Address

我最想弄清楚的最后一件事是如何按优先级排序。这些来源按它们添加的顺序排序,有时较新的来源获得更高的优先级,因此它们不一定按此顺序。

如果有人能够帮助我指出正确的方向,我将不胜感激。

【问题讨论】:

    标签: xml powershell foreach


    【解决方案1】:

    您可以使用计算属性。 这将使您开始:

    $f.Sources.add | Select-Object @{n='Source';e={$_.id}}, @{n='Type';e={$_.type}}, @{n='Priority';e={$_.priority}} | Sort-Object -Property Priority
    

    【讨论】:

    • 效果很好,谢谢!一旦我将 FT 添加到末尾,它就完全符合我的要求!
    猜你喜欢
    • 1970-01-01
    • 2017-07-02
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 2017-08-05
    相关资源
    最近更新 更多