PowerShell中是否存在以下内容:"Select-Object <Property>.<SubProperty>"?

Sim*_*wsi 12 powershell pipeline powershell-4.0

场景:我正在使用Select-Object来访问管道对象的属性,其中一个属性本身就是一个对象.我们称之为PropertyObject.我想访问PropertyObject的属性,比如说Property1.是否有任何好的和干净的方式访问Property1,顺序如下:

...| select-object PropertyObject.Property1
Run Code Online (Sandbox Code Playgroud)

在进行实验时,如果我执行以下操作,我只能让它工作:

...| select-object {$_.PropertyObject.Property1}
Run Code Online (Sandbox Code Playgroud)

如果我想用一个像样的列名显示它,它会变得更加混乱:

...| select-object @{Name="Property1"; Expression={$_.PropertyObject.Property1}}
Run Code Online (Sandbox Code Playgroud)

鉴于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>
Run Code Online (Sandbox Code Playgroud)

将XML加载到XmlDocument中的代码:

$filePath = "C:\Temp\books3.xml"
$xmlDoc = new-object xml
$xmlDoc.load($filePath)
Run Code Online (Sandbox Code Playgroud)

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

$xmlDoc.catalog.book | select author, title, publisher.name
Run Code Online (Sandbox Code Playgroud)

结果:

author                     title                      publisher.name
------                     -----                      --------------
Gambardella, Matthew       XML Developer's Guide
Ralls, Kim                 Midnight Rain
Corets, Eva                Maeve Ascendant
Run Code Online (Sandbox Code Playgroud)

kha*_*edh 23

您可以使用计算属性.一个简短的表格将允许这样:

$xmlDoc.catalog.book | select author, title, {$_.publisher.name}
Run Code Online (Sandbox Code Playgroud)

如果属性名称很重要,则需要将其添加进去.

$xmlDoc.catalog.book | select author, title, @{N="PublisherName";E={$_.publisher.name}}
Run Code Online (Sandbox Code Playgroud)

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


Mat*_*att 5

如果你有一些实际可重复的东西供我们测试会更容易,但我们可以用Get-Items返回来伪造它.

(Get-Item C:\temp\logoutput).Parent.Name
Run Code Online (Sandbox Code Playgroud)

.Parent实际上是一个System.IO.DirectoryInfo对象.我使用PowerShell 3.0点符号来获得nameparent

通过链接select呼叫可以获得相同的结果

Get-Item C:\temp\logoutput | select -expand Parent | Select -Expand name
Run Code Online (Sandbox Code Playgroud)

这当然适用于PowerShell 2.0,但不是简洁的3.0版本.

发布问题编辑

不确定你希望的是什么.你所拥有的工作以你拥有它的方式提取子属性.我只能提供一种可能更直观和友好的替代方法,但结果是一样的.

$xml.catalog.book | ForEach-Object{
    $_ | Add-Member NoteProperty "PublisherName" $_.publisher.name -PassThru}
Run Code Online (Sandbox Code Playgroud)

当然,您可能仍需要使用select将输出限制为所需的属性,但这是另一种选择.