powershell - PowerShell : "Select-Object <Property>.<SubProperty>"? 中可能存在以下情况吗

标签 powershell pipeline powershell-4.0

场景:我使用 Select-Object 访问管道对象的属性,并且这些属性之一本身就是一个对象。我们将其称为 PropertyObject。我想访问该 PropertyObject 的属性,例如 Property1。有没有什么好的和干净的方式来访问Property1,沿着线:

...| select-object PropertyObject.Property1

在实验时,我只有执行以下操作才能使其正常工作:

...| select-object {$_.PropertyObject.Property1}

如果我想用一个合适的列名来显示它,它会变得更加困惑:

...| select-object @{Name="Property1"; Expression={$_.PropertyObject.Property1}}

考虑到 PowerShell 总体上是多么干净和简洁,我不禁认为我错过了一些东西,应该有一种更简洁的方法来访问属性的属性。有吗?

编辑:根据马特的要求,这是具体示例:

我正在读取 XML 文件 Books3.xml:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date inprint="false">2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
      <publisher>
        <name>Simon and Schuster</name>
        <country>USA</country>
        <city>New York</city>
      </publisher>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date inprint="true">2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
      <publisher>
        <name>HarperCollins</name>
        <country>USA</country>
        <city>New York</city>
      </publisher>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date inprint="false">2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
      <publisher>
        <name>Macmillan</name>
        <country>United Kingdom</country>
        <city>London</city>
      </publisher>
   </book>
</catalog>

将 XML 加载到 XmlDocument 中的代码:

$filePath = "C:\Temp\books3.xml"
$xmlDoc = new-object xml
$xmlDoc.load($filePath)

尝试阅读每本书的详细信息:

$xmlDoc.catalog.book | select author, title, publisher.name

结果:

author                     title                      publisher.name
------                     -----                      --------------
Gambardella, Matthew       XML Developer's Guide
Ralls, Kim                 Midnight Rain
Corets, Eva                Maeve Ascendant

最佳答案

您可以使用calculated properties 。简短的形式可以实现这一点:

$xmlDoc.catalog.book | select author, title, {$_.publisher.name}

如果属性名称很重要,您需要添加它们。

$xmlDoc.catalog.book | select author, title, @{N="PublisherName";E={$_.publisher.name}}

其中 N 是名称的缩写,E 是表达式的缩写。

关于powershell - PowerShell : "Select-Object <Property>.<SubProperty>"? 中可能存在以下情况吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29595518/

相关文章:

powershell - 如何使用Powershell向Windows服务器上的域帐户授予SeBatchLogon特权?

java - netty ChannelPipeline 添加最后一个

powershell - 如何在 powershell 中使用 certreq 显示新创建证书的证书缩略图?

powershell - Get-Process -computername 管道输入仅适用于名字

.net命令列表包--在azure管道中格式化json

python - 理解 FeatureUnion (pandas) 工作的困惑

Powershell 管道全部记录

powershell - Enter-PSSession不适用于端口5986

powershell - 重命名项目并覆盖(如果文件名存在)

powershell - 使用 Powershell-(Relay,Connection) 在 Windows Server 中配置 Smtp 虚拟服务器