【发布时间】:2020-02-03 22:01:18
【问题描述】:
背景:
我有一个 PSCustomObject,它是通过 ... | convertfrom-json 转换 JSON 数组创建的。该对象具有许多其他属性值的对象(基本上它是许多 PSCustomObjects 的集合)。
通过了解对象,我知道它至少包含三种不同类型的对象(类型表示具有不同属性的 PSCustomObject)。
问题:
运行 Get-Member I 时只有两种对象类型及其成员,第三种根本没有列出。我知道还有第三种对象类型,因为我可以选择仅在该对象中可用的属性。
注意:
我曾经遇到过类似的问题,有些成员只有在 $object | select... 方法中首先调用时才会出现在 get-member 的结果中,否则它们就不会出现。我当时也没有想通。当前的问题不相同,但可能相关,因为我尝试了$object | select... 的方法,但没有帮助。
注2:
我确实注意到,在尝试发布可重现的代码时,我只得到一种对象类型作为回报,而不是我从invoke-restmethod 得到的两种,这让我的问题变得更大,这里发生了什么,为什么有些对象类型退回了,有些没有。
示例:
get-member 结果示例
$res.address_objects.ipv4 | gm
TypeName: Selected.System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
host NoteProperty System.Management.Automation.PSCustomObject <snip>
name NoteProperty string name=<snip>
uuid NoteProperty string uuid=<snip>
zone NoteProperty string zone=<snip>
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
name NoteProperty string name=<snip>
network NoteProperty System.Management.Automation.PSCustomObject <snip>
uuid NoteProperty string uuid=<snip>
如您所见,这里有两种对象类型,它们都有一些不同的属性名称。
示例:
我转换为对象的示例 Json。
取自@Jawad 的回答。
请注意:此示例不是我的代码的精确副本,因为我的 psobject 是 invoke-restmethod 自动将 json 转换为对象的结果。
$json = @"
{
"address_objects": {
"ipv4": [{
"host": "hostValue",
"name": "hostName",
"uuid": "value",
"zone": "thisZone"
},
{
"name": "NewName",
"network": "newNetwork",
"uuid": "thisUuid"
},
{
"name": "NewName",
"range": "newrange",
"uuid": "thisUuid"
}]
}
}
"@ | ConvertFrom-Json
运行Get-member时我预期的输出:
$json.address_objects.ipv4 | gm
TypeName: Selected.System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
host NoteProperty System.Management.Automation.PSCustomObject <snip>
name NoteProperty string name=<snip>
uuid NoteProperty string uuid=<snip>
zone NoteProperty string zone=<snip>
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
name NoteProperty string name=<snip>
network NoteProperty System.Management.Automation.PSCustomObject <snip>
uuid NoteProperty string uuid=<snip>
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
name NoteProperty string name=<snip>
range NoteProperty System.Management.Automation.PSCustomObject <snip>
uuid NoteProperty string uuid=<snip>
基本上有三个不同的 psCustomObjects,所以get-member 应该列出所有三个。
编辑#1:
感谢评论者编辑,他们是对的,所以我添加了一个可重现的样本并澄清了我的问题。我还没有深入剖析给出的答案。
【问题讨论】:
-
$json.address_objects.ipv4 | ForEach-Object { $_ | Get-Member -MemberType Properties }应该可以完成这项工作。但是,@mklement0 的详尽解释是有效的…… -
@JosefZ,您的命令将显示具有重复项的属性的单个列表,包括值的变化(请注意,输入是“许多 PSCustomObjects 的集合”),而目标是输出不同属性 sets 的列表,即每个不同的“自定义类型”都有一个输出块(正如
Get-Member对真正不同的类型所做的那样)。但是,您可以调整您的命令来回答一个不同的问题:... | ForEach-Object { ($_ | Get-Member -MemberType Properties).Name } | Sort-Object -Unique输出所有输入对象的不同属性名称的列表。
标签: powershell reflection