如果我在哈希表中有一个电子邮件元数据列表,而每封电子邮件在该对象中都有一个带有附件列表的哈希表,就像这样:
$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
关联起来。
答案 0 :(得分:2)
您将要使用计算的属性。
$Emails | Select-Object -Property @{Name='ID';Expression={$_.ID};},@{Name='AttachmentId';Expression={$_.Attachments.ID};}
您可以将Name
和Expression
缩短为n
和e
。
答案 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