PowerShell-显示父对象和子对象的属性

时间:2018-12-12 22:06:07

标签: powershell powershell-v4.0

如果我在哈希表中有一个电子邮件元数据列表,而每封电子邮件在该对象中都有一个带有附件列表的哈希表,就像这样:

$Emails = @{     
            ID          = "E123"; 
            Subject     = "Check this out"; 
            Attachments = @{
                            ID   = "A123"; 
                            Name = "FunnyPic.jpg"
                           }
           }

然后将该哈希表转换为如下所示的PSObject:

$EmailsObject = New-Object -TypeName PSObject -Property $Emails

我想做这样的事情:

$EmailsObject | Select ID, Attachments.ID

我该怎么做?

我想将电子邮件的ID与相关附件的ID关联起来。

2 个答案:

答案 0 :(得分:2)

您将要使用计算的属性。

$Emails | Select-Object -Property @{Name='ID';Expression={$_.ID};},@{Name='AttachmentId';Expression={$_.Attachments.ID};}

您可以将NameExpression缩短为ne

See this article for more

答案 1 :(得分:1)

如果使用Select-Object,则需要使用计算所得的属性。像这样的东西

$Emails = @{     
            ID          = "E123"; 
            Subject     = "Check this out"; 
            Attachments = @{
                            ID   = "A123"; 
                            Name = "FunnyPic.jpg"
                           }
           }
$EmailsObject = New-Object -TypeName PSObject -Property $Emails

$EmailsObject |
    Select-Object ID,
        @{
        n = 'AttID'
        e = {$_.Attachments.ID}
        }

输出...

ID   AttID
--   -----
E123 A123