gpt4 book ai didi

powershell - select-object -expandproperty with null 属性

转载 作者:行者123 更新时间:2023-12-02 22:27:14 26 4
gpt4 key购买 nike

我有一组具有 id、用户名、电子邮件、current_sign_in_at、身份等属性的对象。其中 Identities 属性是具有两个属性的对象数组。这将是一个对象的 json 表示:

{
"id": 45,
"name": "Emilio Roche",
"username": "EROCHE",
"state": "active",
"identities": [
{
"provider": "ldapmain",
"extern_uid": "cn=roche\\, emilio,ou=xxxxxxxxxx"
}
]
}

但是列表中的某些元素没有 identities 属性。所以,当我这样做时:
Get-Collection | Select-Object id, username  -ExpandProperty identities

我只得到那些具有 identities 属性的元素。我需要所有实体,有或没有 identities 属性

最佳答案

如果没有太多的属性需要处理,你可以使用这样的方法:

Get-Collection | Select-Object id, 
username,
@{n='provider';e={$_.identities.provider}},
@{n='extern_uid';e={$_.identities.extern_uid}}

这将返回 $null关于属性 providerextern_uid对于那些没有 identities 的对象属性(property):
id username provider extern_uid                     
-- -------- -------- ----------
45 EROCHE ldapmain cn=roche\, emilio,ou=xxxxxxxxxx
46 EROCHE

编辑

正如 mklement0 指出的那样,如果 identities 属性包含多个对象,则该方法不起作用。

mklement0 的答案对这个问题有一个优雅的解决方案,应该是公认的答案。

关于powershell - select-object -expandproperty with null 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53611903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com